mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 04:45:49 +00:00
Updating using GitHub API 2025-06-19-15:31
This commit is contained in:
parent
55ae3ad427
commit
c0c2985bd5
1 changed files with 8 additions and 5 deletions
|
@ -10,7 +10,7 @@
|
|||
* 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
|
||||
* See the License for specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
@ -47,6 +47,9 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|||
class OwnerController {
|
||||
|
||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
private static final String ERROR_ATTRIBUTE = "error";
|
||||
private static final String ERROR_MESSAGE_CREATE = "There was an error in creating the owner.";
|
||||
private static final String ERROR_MESSAGE_UPDATE = "There was an error in updating the owner.";
|
||||
|
||||
private final OwnerRepository owners;
|
||||
|
||||
|
@ -75,7 +78,7 @@ class OwnerController {
|
|||
@PostMapping("/owners/new")
|
||||
public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) {
|
||||
if (result.hasErrors()) {
|
||||
redirectAttributes.addFlashAttribute("error", "There was an error in creating the owner.");
|
||||
redirectAttributes.addFlashAttribute(ERROR_ATTRIBUTE, ERROR_MESSAGE_CREATE);
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
|
@ -139,13 +142,13 @@ class OwnerController {
|
|||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (result.hasErrors()) {
|
||||
redirectAttributes.addFlashAttribute("error", "There was an error in updating the owner.");
|
||||
redirectAttributes.addFlashAttribute(ERROR_ATTRIBUTE, ERROR_MESSAGE_UPDATE);
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
if (owner.getId() != ownerId) {
|
||||
result.rejectValue("id", "mismatch", "The owner ID in the form does not match the URL.");
|
||||
redirectAttributes.addFlashAttribute("error", "Owner ID mismatch. Please try again.");
|
||||
redirectAttributes.addFlashAttribute(ERROR_ATTRIBUTE, "Owner ID mismatch. Please try again.");
|
||||
return "redirect:/owners/{ownerId}/edit";
|
||||
}
|
||||
|
||||
|
@ -170,4 +173,4 @@ class OwnerController {
|
|||
return mav;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue