I would like to ask more experienced developers to review my code, it's my first app, self-made, I know there is a lot to improve, it would be great if you could just point the main errors.
I'm aware that my implementation is not universal, but I couldnt get past it. My edit option with usage of DTO seems shi*y to me, details can be found in the code.
https://github.com/filemonczyk/crud
Thanks to all in advance
@Controller
public class AnimalsController {
@Autowired
private AnimalDAO animalDAO;
@RequestMapping("")
public String startPage()
{
return "home";
}
//gives a list of animals of specified type (dog,cat,snake)
@RequestMapping("write{animal}")
public String getAnimalList(Model model, @PathVariable("animal") String animal ) {
model.addAttribute("animalName", animalDAO.getAnimalList(animal));
return "list";
}
@RequestMapping("{animal}/{name}/{id}/delete")
public String dropAnimal(@PathVariable("id") int id, HttpServletResponse response) throws IOException{
animalDAO.delete(id);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
//following 3 methodes are adding new animals to the database, I have used here DTO, had no idea how I can turn it into more flexible solutions
@RequestMapping("addCat")
public String addAnimal(HttpServletRequest request,HttpServletResponse response, @ModelAttribute("catDto") @Valid CatDTO catDto, BindingResult result) throws IOException{
if(request.getMethod().equalsIgnoreCase("post") && !result.hasErrors()){
Cat cat = new Cat();
cat.setName(catDto.getName());
cat.setAge(catDto.getAge());
cat.setDateOfBirth(catDto.getBirthDay());
cat.setBreed(catDto.getBreed());
cat.setWeight(catDto.getWeight());
cat.setFurColor(catDto.getFurColor());
animalDAO.addAnimal(cat);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
return "addCat";
}
@RequestMapping("addDog")
public String addAnimal(HttpServletRequest request,HttpServletResponse response, @ModelAttribute("dogDto") @Valid DogDTO dogDto, BindingResult result) throws IOException{
if(request.getMethod().equalsIgnoreCase("post") && !result.hasErrors()){
Dog dog = new Dog();
dog.setName(dogDto.getName());
dog.setAge(dogDto.getAge());
dog.setDateOfBirth(dogDto.getBirthDay());
dog.setWeight(dogDto.getWeight());
dog.setTrained(dogDto.isTrained());
dog.setOwnerName(dogDto.getOwnerName());
animalDAO.addAnimal(dog);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
return "addDog";
}
@RequestMapping("addSnake")
public String addAnimal(HttpServletRequest request,HttpServletResponse response, @ModelAttribute("snakeDto") @Valid SnakeDTO snakeDto, BindingResult result) throws IOException{
if(request.getMethod().equalsIgnoreCase("post") && !result.hasErrors()){
Snake snake = new Snake();
snake.setName(snakeDto.getName());
snake.setAge(snakeDto.getAge());
snake.setDateOfBirth(snakeDto.getBirthDay());
snake.setWeight(snakeDto.getWeight());
snake.setLength(snakeDto.getLength());
snake.setVenomous(snakeDto.isVenomous());
animalDAO.addAnimal(snake);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
return "addSnake";
}
// here is the core of my doubts, i wasnt able to configure view-model communication in that way I was not forced
//to "hard-type" 3 different @ModelAttributes with 3 different object types.
@RequestMapping("{animal}/{name}/{id}")
public String getAnimal(HttpServletResponse response,Model model, HttpServletRequest request, @PathVariable("animal") String name, @PathVariable("id") int id,
@ModelAttribute("snakeDto") SnakeDTO snakeDto, @ModelAttribute("catDto") CatDTO catDto ,@ModelAttribute("dogDto") DogDTO dogDto) throws IOException{
if(request.getMethod().equalsIgnoreCase("post")&&name.equalsIgnoreCase("snake")){
Snake an = (Snake)animalDAO.getAnimalById(id);
an.setAge(snakeDto.getAge());
an.setDateOfBirth(snakeDto.getBirthDay());
an.setLength(snakeDto.getLength());
an.setName(snakeDto.getName());
an.setWeight(snakeDto.getWeight());
an.setVenomous(snakeDto.isVenomous());
animalDAO.updateAnimal(an);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
if(request.getMethod().equalsIgnoreCase("post")&&name.equalsIgnoreCase("cat")){
Cat an = (Cat)animalDAO.getAnimalById(id);
an.setAge(catDto.getAge());
an.setDateOfBirth(catDto.getBirthDay());
an.setBreed(catDto.getBreed());
an.setName(catDto.getName());
an.setWeight(catDto.getWeight());
an.setFurColor(catDto.getFurColor());
animalDAO.updateAnimal(an);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
if(request.getMethod().equalsIgnoreCase("post")&&name.equalsIgnoreCase("dog")){
Dog an = (Dog)animalDAO.getAnimalById(id);
an.setAge(dogDto.getAge());
an.setDateOfBirth(dogDto.getBirthDay());
an.setTrained(dogDto.isTrained());
an.setName(dogDto.getName());
an.setWeight(dogDto.getWeight());
an.setOwnerName(dogDto.getOwnerName());
animalDAO.updateAnimal(an);
response.sendRedirect("http://localhost:8080/crud");
return "home";
}
model.addAttribute("animalDetails", animalDAO.getAnimalById(id));
return "details"+name;
}
}
this is corresponding view:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" methodAttribute="catDto">
Insert Cat's name
<input type="text" name="name"/><br>
Insert Cat's age
<input type="text" name="age"/><br>
Insert Cat's date of birth (dd-mm-yyyy)
<input type="text" name="birthDay"><br>
Insert Cat's weight
<input type="text" name="weight"/><br>
Insert Cat's breed?
<input type="text" name="breed"/><br>
Insert Cat's fur color
<input type="text" name="furColor"/> <br>
<input type="submit" value="add"/>
</form>
</body>
</html>