Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Curso private controller and Curso refactor #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package br.com.openenade.api.categoriaadmin;


import com.fasterxml.jackson.annotation.JsonValue;

public enum CategoriaAdmin {
PUBLICO("Publico"),PRIVADO("Privado");

PUBLICO("Publico"), PRIVADO("Privado");

private String value;

private CategoriaAdmin() {

}

private CategoriaAdmin(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return this.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
@CrossOrigin(origins = "*")
@RequestMapping(path = CategoriaAdminController.ENDPOINT)
public class CategoriaAdminController {

public static final String ENDPOINT = "/categorias-admin";

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody ResponseEntity<CategoriaAdmin[]> index() {

return new ResponseEntity<>(CategoriaAdmin.values(), HttpStatus.OK);
}

}
9 changes: 3 additions & 6 deletions src/main/java/br/com/openenade/api/curso/Curso.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.NotBlank;
import br.com.openenade.api.modalidade.Modalidade;
import javax.persistence.IdClass;


@Entity
@IdClass(CursoId.class)
public class Curso {

@NotBlank(message = "'nome' não pode ser vazio.")
@NotBlank(message = "Curso 'nome' nao pode ser vazio")
private String nome;

@Id
@Positive(message = "'codigoArea' precisa ser positivo.")
@Positive(message = "Curso 'codigoArea' precisa ser positivo")
private Long codigoArea;

@Id
@Enumerated(EnumType.STRING)
@NotNull(message = "Você precisa especificar uma 'modalidade' de ensino.")
@Enumerated(EnumType.ORDINAL)
private Modalidade modalidade;

public Curso() {
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/br/com/openenade/api/curso/CursoController.java

This file was deleted.

23 changes: 14 additions & 9 deletions src/main/java/br/com/openenade/api/curso/CursoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,37 @@
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import br.com.openenade.api.exceptions.ResourceNotFound;
import br.com.openenade.api.modalidade.Modalidade;


@Service
public class CursoService {

@Autowired
private CursoRepository repository;

public Curso save(Curso curso) {
public Curso addCurso(Curso curso) {
return this.repository.save(curso);
}

public List<Curso> getAll() {
return this.repository.findAll();
}

public Optional<Curso> getByCodigo(Long codigo, Modalidade modalidade) {
public Curso getByCodigo(Long codigo, Modalidade modalidade) {
CursoId cursoId = new CursoId(codigo, modalidade);
return this.repository.findById(cursoId);

Optional<Curso> optCurso = this.repository.findById(cursoId);
if (optCurso.isPresent()) {
return optCurso.get();
} else {
throw new ResourceNotFound("Cannot find Curso with Codigo [" + Long.toString(codigo)
+ "] and Modalidade [" + modalidade.getValue() + "]");
}
}
// unutilized
public void deleteCursoById(CursoId id) {
this.repository.deleteById(id);

public void deleteById(CursoId cursoId) {
this.getByCodigo(cursoId.getCodigoArea(), cursoId.getModalidade());
this.repository.deleteById(cursoId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.openenade.api.curso;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import br.com.openenade.api.modalidade.ModalidadeService;

@RestController
@RequestMapping(path = PublicCursoController.ENDPOINT)
public class PrivateCursoController {

@Autowired
private CursoService service;

@ResponseBody
@PostMapping
public Curso addCurso(@Valid @RequestBody Curso newCurso) {
return this.service.addCurso(newCurso);
}

@DeleteMapping(path = "/{codigo}/{modalidade}")
public void deleteCursoByCodigo(@PathVariable(name = "codigo") Long codigo,
@PathVariable(name = "modalidade") Integer modalidadeId) {
this.service
.deleteById(new CursoId(codigo, ModalidadeService.getModalidadeById(modalidadeId)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package br.com.openenade.api.curso;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import br.com.openenade.api.modalidade.ModalidadeService;

@RestController
@CrossOrigin("*")
@RequestMapping(path = PublicCursoController.ENDPOINT)
public class PublicCursoController {

public static final String ENDPOINT = "cursos";

@Autowired
private CursoService service;

@GetMapping
@ResponseBody
public List<Curso> getAll() {
return this.service.getAll();
}

@ResponseBody
@GetMapping(path = "/{codigo}/{modalidade}")
public Curso getCursoByCodigo(@PathVariable(name = "codigo") Long codigo,
@PathVariable(name = "modalidade") Integer modalidadeId) {
return this.service.getByCodigo(codigo, ModalidadeService.getModalidadeById(modalidadeId));
}

}
9 changes: 6 additions & 3 deletions src/main/java/br/com/openenade/api/modalidade/Modalidade.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
public enum Modalidade {

@Enumerated(EnumType.ORDINAL)
EDUCACAO_PRESENCIAL("Educação Presencial"), EDUCACAO_A_DISTANCIA("Educação à Distância");
EDUCACAO_PRESENCIAL("Educação Presencial", 0), EDUCACAO_A_DISTANCIA("Educação à Distância", 1);

private String value;
private Integer id;

private Modalidade() {

}

private Modalidade(String value) {
private Modalidade(String value, Integer id) {
this.value = value;
this.id = id;
}

@JsonValue
public String getValue() {
return this.value;
return this.value + " - " + this.id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class ModalidadeController {
public static final String ENDPOINT = "/modalidades";

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody ResponseEntity<Modalidade[]> index() {
public @ResponseBody ResponseEntity<Modalidade[]> getAllModalidades() {
return new ResponseEntity<>(Modalidade.values(), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.openenade.api.modalidade;

import br.com.openenade.api.exceptions.ResourceNotFound;

public class ModalidadeService {

public static Modalidade getModalidadeById(Integer id) {
Modalidade[] modalidades = Modalidade.values();
if (id < 0 || id >= modalidades.length) {
throw new ResourceNotFound(
"Cannot find Modalidade with Id [" + Integer.toString(id) + "]");
} else {
return modalidades[id];
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ public Universidade save(Universidade universidade) {
}

public Universidade addCurso2Universidade(Universidade universidade, Curso curso) {
Optional<Curso> optCurso =
this.cursoService.getByCodigo(curso.getCodigoArea(), curso.getModalidade());
if (optCurso.isPresent()) {
universidade.addCurso(optCurso.get());
return this.repository.saveAndFlush(universidade);
} else {
throw new ResourceNotFound(curso.toString());
}
curso = this.cursoService.getByCodigo(curso.getCodigoArea(), curso.getModalidade());
universidade.addCurso(curso);
return this.repository.saveAndFlush(universidade);
}

public Collection<Universidade> getAll() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/br/com/openenade/api/BaseUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void clean() {
for (Curso curso : this.cursoService.getAll()) {

CursoId id = new CursoId(curso.getCodigoArea(), curso.getModalidade());
this.cursoService.deleteCursoById(id);
this.cursoService.deleteById(id);
}
for (Ano ano : this.anoService.getAllAnos()) {
this.anoService.deleteAno(ano.getAno());
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/br/com/openenade/api/curso/CursoServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CursoServiceTests extends BaseUnitTest {

@Test
public void testSaveBasic() {
this.cursoService.save(new Curso("CC", 13, Modalidade.EDUCACAO_PRESENCIAL));
this.cursoService.addCurso(new Curso("CC", 13, Modalidade.EDUCACAO_PRESENCIAL));
}

@Test
Expand All @@ -35,10 +35,10 @@ public void testGetAllBasic() {

Curso e = new Curso("História", 13, Modalidade.EDUCACAO_A_DISTANCIA);

this.cursoService.save(a);
this.cursoService.save(b);
this.cursoService.save(c);
this.cursoService.save(d);
this.cursoService.addCurso(a);
this.cursoService.addCurso(b);
this.cursoService.addCurso(c);
this.cursoService.addCurso(d);

List<Curso> cursos = this.cursoService.getAll();

Expand All @@ -63,13 +63,13 @@ public void testGetByCodigo() {
Curso b = new Curso(cursos[1], 5, modalidade);
Curso c = new Curso(cursos[2], 7, modalidade);

this.cursoService.save(a);
this.cursoService.save(b);
this.cursoService.save(c);
this.cursoService.addCurso(a);
this.cursoService.addCurso(b);
this.cursoService.addCurso(c);

Curso aa = this.cursoService.getByCodigo(3L, modalidade).get();
Curso bb = this.cursoService.getByCodigo(5L, modalidade).get();
Curso cc = this.cursoService.getByCodigo(7L, modalidade).get();
Curso aa = this.cursoService.getByCodigo(3L, modalidade);
Curso bb = this.cursoService.getByCodigo(5L, modalidade);
Curso cc = this.cursoService.getByCodigo(7L, modalidade);

assertEquals(a, aa);
assertEquals(b, bb);
Expand Down
Loading