Updating using GitHub API 2025-06-19-15:31

This commit is contained in:
nidhi-vm 2025-06-19 15:31:15 -04:00
parent 55ae3ad427
commit c0c2985bd5

View file

@ -10,7 +10,7 @@
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.owner; package org.springframework.samples.petclinic.owner;
@ -47,6 +47,9 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
class OwnerController { class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; 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; private final OwnerRepository owners;
@ -75,7 +78,7 @@ class OwnerController {
@PostMapping("/owners/new") @PostMapping("/owners/new")
public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) { public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) {
if (result.hasErrors()) { 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; 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, public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (result.hasErrors()) { 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; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} }
if (owner.getId() != ownerId) { if (owner.getId() != ownerId) {
result.rejectValue("id", "mismatch", "The owner ID in the form does not match the URL."); 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"; return "redirect:/owners/{ownerId}/edit";
} }
@ -170,4 +173,4 @@ class OwnerController {
return mav; return mav;
} }
} }