From 12eed780b91a2a3ed90b67d1dd8183af1a5b3f00 Mon Sep 17 00:00:00 2001 From: cmullercejas Date: Wed, 5 Feb 2020 16:06:31 +0100 Subject: [PATCH] starting to creare user service and controller --- .../petclinic/service/UserService.java | 107 +++++++++++++ .../samples/petclinic/web/UserController.java | 145 ++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 src/main/java/org/springframework/samples/petclinic/service/UserService.java create mode 100644 src/main/java/org/springframework/samples/petclinic/web/UserController.java diff --git a/src/main/java/org/springframework/samples/petclinic/service/UserService.java b/src/main/java/org/springframework/samples/petclinic/service/UserService.java new file mode 100644 index 00000000000..ff9e9a0210e --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/service/UserService.java @@ -0,0 +1,107 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.service; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.dao.DataAccessException; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.model.User; +import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.repository.OwnerRepository; +import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.UserRepository; +import org.springframework.samples.petclinic.repository.VetRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class UserService { + + private UserRepository userRepository; + +// private VetRepository vetRepository; +// +// private OwnerRepository ownerRepository; +// +// private VisitRepository visitRepository; + + @Autowired + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; +// this.vetRepository = vetRepository; +// this.ownerRepository = ownerRepository; +// this.visitRepository = visitRepository; + } + +// @Transactional(readOnly = true) +// public Collection findPetTypes() throws DataAccessException { +// return petRepository.findPetTypes(); +// } +// +// @Transactional(readOnly = true) +// public Owner findOwnerById(int id) throws DataAccessException { +// return ownerRepository.findById(id); +// } +// +// @Transactional(readOnly = true) +// public Collection findOwnerByLastName(String lastName) throws DataAccessException { +// return ownerRepository.findByLastName(lastName); +// } + + @Transactional + public void saveUser(User user) throws DataAccessException { + userRepository.save(user); + } + +// @Transactional +// public void saveVisit(Visit visit) throws DataAccessException { +// visitRepository.save(visit); +// } +// +// @Transactional(readOnly = true) +// public Pet findPetById(int id) throws DataAccessException { +// return petRepository.findById(id); +// } +// +// @Transactional +// public void savePet(Pet pet) throws DataAccessException { +// petRepository.save(pet); +// } +// +// @Transactional(readOnly = true) +// @Cacheable(value = "vets") +// public Collection findVets() throws DataAccessException { +// return vetRepository.findAll(); +// } +// +// public Collection findVisitsByPetId(int petId) { +// return visitRepository.findByPetId(petId); +// } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/web/UserController.java b/src/main/java/org/springframework/samples/petclinic/web/UserController.java new file mode 100644 index 00000000000..6550d90ac01 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/UserController.java @@ -0,0 +1,145 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.web; + +import java.util.Collection; +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.model.User; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.samples.petclinic.service.UserService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +public class UserController { + + private static final String VIEWS_USER_CREATE_OR_UPDATE_FORM = "users/createOrUpdateOwnerForm"; + + private final UserService userService; + + @Autowired + public UserController(UserService userService) { + this.userService = userService; + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping(value = "/users/new") + public String initCreationForm(Map model) { + User user = new User(); + model.put("user", user); + return VIEWS_USER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping(value = "/users/new") + public String processCreationForm(@Valid User user, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_USER_CREATE_OR_UPDATE_FORM; + } + else { + this.userService.saveUser(user); +// return "redirect:/owners/" + owner.getId(); //pensar a donde redirigir + + // habrĂ­a que crear el authority "owner" o "veterinary" con el que haya elegido el usuario + + return "redirect:/"; //pensar a donde redirigir + } + } + +// @GetMapping(value = "/owners/find") +// public String initFindForm(Map model) { +// model.put("owner", new Owner()); +// return "owners/findOwners"; +// } +// +// @GetMapping(value = "/owners") +// public String processFindForm(Owner owner, BindingResult result, Map model) { +// +// // allow parameterless GET request for /owners to return all records +// if (owner.getLastName() == null) { +// owner.setLastName(""); // empty string signifies broadest possible search +// } +// +// // find owners by last name +// Collection results = this.clinicService.findOwnerByLastName(owner.getLastName()); +// if (results.isEmpty()) { +// // no owners found +// result.rejectValue("lastName", "notFound", "not found"); +// return "owners/findOwners"; +// } +// else if (results.size() == 1) { +// // 1 owner found +// owner = results.iterator().next(); +// return "redirect:/owners/" + owner.getId(); +// } +// else { +// // multiple owners found +// model.put("selections", results); +// return "owners/ownersList"; +// } +// } +// +// @GetMapping(value = "/owners/{ownerId}/edit") +// public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { +// Owner owner = this.clinicService.findOwnerById(ownerId); +// model.addAttribute(owner); +// return VIEWS_USER_CREATE_OR_UPDATE_FORM; +// } +// +// @PostMapping(value = "/owners/{ownerId}/edit") +// public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, +// @PathVariable("ownerId") int ownerId) { +// if (result.hasErrors()) { +// return VIEWS_USER_CREATE_OR_UPDATE_FORM; +// } +// else { +// owner.setId(ownerId); +// this.clinicService.saveOwner(owner); +// return "redirect:/owners/{ownerId}"; +// } +// } +// +// /** +// * Custom handler for displaying an owner. +// * @param ownerId the ID of the owner to display +// * @return a ModelMap with the model attributes for the view +// */ +// @GetMapping("/owners/{ownerId}") +// public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { +// ModelAndView mav = new ModelAndView("owners/ownerDetails"); +// mav.addObject(this.clinicService.findOwnerById(ownerId)); +// return mav; +// } + +}