Skip to content

Commit

Permalink
animal service adapted for controller and rest controller
Browse files Browse the repository at this point in the history
  • Loading branch information
CadenaR committed Mar 7, 2020
1 parent c6b3df8 commit dc4a963
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -23,39 +21,48 @@
import org.springframework.web.multipart.MultipartFile;
import es.sidelab.animalshelter.Animal;
import es.sidelab.animalshelter.AnimalRepository;
import es.sidelab.animalshelter.UserShelterComponent;
import es.sidelab.animalshelter.controllers.ImageService;
import es.sidelab.animalshelter.services.AnimalService;

@RestController
@RequestMapping("/api/animals")
public class APIanimalController {

private Map<Long, Animal> animals = new ConcurrentHashMap<>();
@Autowired
private ImageService imageService;

@Autowired
private AnimalRepository animalRepository;

@Autowired
private AnimalService animalService;

@Autowired
private UserShelterComponent loggeduser;

@GetMapping("/")
public Collection<Animal> animals() {
return animals.values();
return animalService.findAll();
}

@PostMapping("/addAnimal")
@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public Animal newAnimal(@RequestBody Animal animal) {
animals.put(animal.getIdAnimal(), animal);
animal.setShelterOwner(loggeduser.getShelter());
animalService.save(animal);
return animal;
}

@PutMapping("/{id}")
public ResponseEntity<Animal> updateAnimal(@PathVariable long id, @RequestBody Animal updatedAnimal) {

Animal animal = animals.get(id);
Animal animal = animalService.findByAnimalId(id);

if (animal != null) {

updatedAnimal.setIdAnimal(id);
animals.put(id, updatedAnimal);
animalService.save(updatedAnimal);

return new ResponseEntity<>(updatedAnimal, HttpStatus.OK);
} else {
Expand All @@ -66,7 +73,7 @@ public ResponseEntity<Animal> updateAnimal(@PathVariable long id, @RequestBody A
@GetMapping("/{id}")
public ResponseEntity<Animal> getAnimal(@PathVariable long id) {

Animal animal = animals.get(id);
Animal animal = animalService.findByAnimalId(id);

if (animal != null) {
return new ResponseEntity<>(animal, HttpStatus.OK);
Expand All @@ -78,7 +85,7 @@ public ResponseEntity<Animal> getAnimal(@PathVariable long id) {
@DeleteMapping("/{id}")
public ResponseEntity<Animal> deleteAnimal(@PathVariable long id) {

Animal animal = animals.remove(id);
Animal animal = animalService.findByAnimalId(id);

if (animal != null) {
return new ResponseEntity<>(animal, HttpStatus.OK);
Expand All @@ -92,17 +99,18 @@ public ResponseEntity<Animal> deleteAnimal(@PathVariable long id) {
public Animal setAnimalImage(@RequestParam(value = "files", required = false) MultipartFile file,
@PathVariable long id) throws IOException {

Animal animal = animals.get(id);
Animal animal = animalService.findByAnimalId(id);
imageService.saveImage("animals", animal.getIdAnimal(), file);
animal.setAnimalPhoto("image-" + animal.getIdAnimal() + ".jpg");

animalService.save(animal);

return animal;
}

@GetMapping("/{id}/image")
public ResponseEntity<String> getAnimalimage(@PathVariable long id) {

Animal animal = animals.get(id);
Animal animal = animalService.findByAnimalId(id);

if (animal != null) {
return new ResponseEntity<>(animal.getAnimalPhoto(), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class APIshelterController {
public Collection<Shelter> shelter() {
return service.findAll();
}

@PostMapping("/")
public ResponseEntity<Shelter> newShelter(@RequestBody Shelter shelter) {
if (service.save(shelter)) {
return new ResponseEntity<>(shelter, HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.ALREADY_REPORTED);
}
}

@PutMapping("/{id}")
public ResponseEntity<Shelter> updateShelter(@PathVariable long id, @RequestBody Shelter updatedShelter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ protected void configure(HttpSecurity http) throws Exception {

// URLs that need authentication to access to it
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/users/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/users/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/users/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/users/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.DELETE, "/api/users/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/createAnimal/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/animalform/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/request/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/request/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/shelters/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/shelters/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/shelters/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.DELETE, "/api/shelters/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/animals/**").hasAnyRole("USER","SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/animals/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/animals/**").hasRole("SHELTER");
http.authorizeRequests().antMatchers(HttpMethod.DELETE, "/api/animals/**").hasRole("SHELTER");


// Other URLs can be accessed without authentication
http.authorizeRequests().anyRequest().permitAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import es.sidelab.animalshelter.Animal;
import es.sidelab.animalshelter.AnimalRepository;
import es.sidelab.animalshelter.UserShelterComponent;
import es.sidelab.animalshelter.WebUser;
import es.sidelab.animalshelter.services.AnimalService;

@Controller
public class AnimalFormController extends ModelAttributeController{

@Autowired
private AnimalRepository animalRepository;
private AnimalService service;

@Autowired
private UserShelterComponent userShelterComponent;
Expand All @@ -37,7 +37,7 @@ public class AnimalFormController extends ModelAttributeController{
@RequestMapping("/animals")
public String addAnimal(Model model, HttpServletRequest request) {// in animal html it will include information

List<Animal> animal = (List<Animal>) animalRepository.findAll();
List<Animal> animal = service.findAll();

model.addAttribute("animal", animal);
return "animals";
Expand All @@ -54,17 +54,17 @@ public String createAnimal(Model model, HttpServletRequest request, @RequestPara
@RequestParam String animalDescription, @RequestParam String animalSize) throws IOException {

Animal animal = new Animal(animalName, animalAge, animalType, animalSize, animalDescription);
animalRepository.save(animal); // It's saved to get the id
service.save(animal); // It's saved to get the id
imgService.saveImage("animal", animal.getIdAnimal(), imagenFile);
animal.setAnimalPhoto("image-" + animal.getIdAnimal() + ".jpg");
animal.setShelterOwner(userShelterComponent.getShelter());
animalRepository.save(animal);
service.save(animal);
return "animalform";
}

@RequestMapping(value = "/animal") //Returns the type filter search
public String animalFilterProcess(Model model, @ModelAttribute("filter") String filter, HttpServletRequest request) {
List<Animal> animal = (List<Animal>) animalRepository.findAll();
List<Animal> animal = service.findAll();
List<Animal> animalFilter = new ArrayList<Animal>(animal);
for (Animal mem : animal) {
if (filter.matches("others")) {
Expand All @@ -87,7 +87,7 @@ public String animalFilterProcess(Model model, @ModelAttribute("filter") String

@RequestMapping(value = "/animalname") //Returns the animalName search
public String animalSearchByName(Model model, @ModelAttribute("hola") String filter, HttpServletRequest request) {
List<Animal> animal = (List<Animal>) animalRepository.findAll();
List<Animal> animal = (List<Animal>) service.findAll();
List<Animal> animalFilter = new ArrayList<Animal>(animal);
for (Animal mem : animal) {

Expand All @@ -105,7 +105,7 @@ public String userSuitedanimal(Model model, @ModelAttribute("hola") String filte

List<Animal> animalFilter = new ArrayList<Animal>();
WebUser userActive= userShelterComponent.getUser();
for (Animal mem : animalRepository.findAll()) {
for (Animal mem : service.findAll()) {

if (mem.getAnimalDimensions()<= userActive.getUserCapacity()) {
animalFilter.add(mem);
Expand All @@ -119,7 +119,7 @@ public String userSuitedanimal(Model model, @ModelAttribute("hola") String filte
@GetMapping("/animal/{idAnimal}")
public String showAnimalInfo(Model model, @PathVariable long idAnimal, HttpServletRequest request) {

Optional<Animal> animal = animalRepository.findByIdAnimal(idAnimal);
Optional<Animal> animal = Optional.of(service.findByAnimalId(idAnimal));
if (animal.isPresent()) {
model.addAttribute("animal", animal.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public Animal findByAnimalId(long id) {
return repository.getOne(id);
}

public Animal findByShelterEmail(String name) {
return repository.findByAnimalName(name);
}

public List<Animal> findAll() {
return repository.findAll();
}
Expand All @@ -28,10 +24,6 @@ public void save(Animal animal) {
repository.save(animal);
}

public void update(Animal animal) {
repository.save(animal);
}

public void delete(long id) {
repository.deleteById(id);
}
Expand Down

0 comments on commit dc4a963

Please sign in to comment.