From fcfeaf0979979e8391293ef8021757008ae3a9cd Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Tue, 1 Feb 2022 15:44:03 +0100 Subject: [PATCH] Fixed equals --- .../server/controller/EventController.java | 96 +++++++++++++++---- .../main/java/com/vpr/server/data/Event.java | 3 +- .../main/java/com/vpr/server/data/User.java | 2 +- .../java/com/vpr/server/data/UserEvent.java | 9 +- 4 files changed, 90 insertions(+), 20 deletions(-) diff --git a/server/src/main/java/com/vpr/server/controller/EventController.java b/server/src/main/java/com/vpr/server/controller/EventController.java index f8d709b..ba1e9e2 100644 --- a/server/src/main/java/com/vpr/server/controller/EventController.java +++ b/server/src/main/java/com/vpr/server/controller/EventController.java @@ -15,6 +15,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; @Controller @@ -72,7 +73,7 @@ public class EventController { } eventRepository.deleteUserEventsById(userId, eventId, date); - if(eventDAO.getAllEventsWithId(eventId).size() == 0){ + if (eventDAO.getAllEventsWithId(eventId).size() == 0) { eventRepository.deleteById(eventId); } @@ -115,7 +116,10 @@ public class EventController { ) { User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]); if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) { - return new ResponseEntity<>("Du hast keine Rechte um den Termin zu bearbeiten", HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>( + "Du hast keine Rechte um den Termin zu bearbeiten", + HttpStatus.UNAUTHORIZED + ); } List eventList = eventDAO.getAllEventsWithIdAndDate(userId, eventId, date); @@ -124,14 +128,29 @@ public class EventController { return new ResponseEntity<>("Der Termin exestiert nicht in der Datenbank", HttpStatus.BAD_REQUEST); } if (eventList.size() > 1) { - return new ResponseEntity<>("Drr Termin ist doppelt vorhanden. (Um das zu lösen versuche den Termin zu löschen und erneut zu erstellen)", HttpStatus.BAD_REQUEST); + return new ResponseEntity<>( + "Der Termin ist doppelt vorhanden. " + + "(Um das zu lösen versuche den Termin zu löschen und erneut zu erstellen)", + HttpStatus.BAD_REQUEST + ); } - ResponseEntity error = createEventAndUserEvent(userId, newDate, newName, newStart, newEnd, newPriority, newIsFullDay, newIsPrivate); + ResponseEntity error = createEventAndUserEvent( + userId, + newDate, + newName, + newStart, + newEnd, + newPriority, + newIsFullDay, + newIsPrivate, + eventId + ); + if (error != null) return error; eventRepository.deleteUserEventsById(userId, eventId, date); - if(eventDAO.getAllEventsWithId(eventId).size() == 0){ + if (eventDAO.getAllEventsWithId(eventId).size() == 0) { eventRepository.deleteById(eventId); } @@ -139,9 +158,42 @@ public class EventController { return new ResponseEntity<>("", HttpStatus.OK); } - private ResponseEntity createEventAndUserEvent(long userId, String date, String name, String start, String end, Integer priority, Boolean isFullDay, Boolean isPrivate) { + private ResponseEntity createEventAndUserEvent( + long userId, + String date, + String name, + String start, + String end, + Integer priority, + Boolean isFullDay, + Boolean isPrivate + ) { + return createEventAndUserEvent( + userId, + date, + name, + start, + end, + priority, + isFullDay, + isPrivate, + -1 + ); + } + + private ResponseEntity createEventAndUserEvent( + long userId, + String date, + String name, + String start, + String end, + Integer priority, + Boolean isFullDay, + Boolean isPrivate, + long oldEventId + ) { User user = userRepository.findById(userId); - if(user == null){ + if (user == null) { return new ResponseEntity<>("UserId nicht korrekt", HttpStatus.BAD_REQUEST); } @@ -162,15 +214,19 @@ public class EventController { userEvent.setUser(user); List userEvents = userEventRepository.findByUserIdAndDate(user.getId(), userEvent.getDate()); - System.out.println(userEvents.size() + ""); - if( - (event.isFullDay() && userEvents.size() > 0) && - !(userEvents.size() == 1 && userEvent.equals(userEvents.get(0))) - ){ - return new ResponseEntity<>("Es gibt bereits Termine am " + userEvent.getDate(), HttpStatus.BAD_REQUEST); - }else{ - for(UserEvent ue : userEvents){ - if(ue.getEvent().isFullDay()){ + + boolean isFullDayButDayHasEvents = event.isFullDay() && userEvents.size() > 0; + boolean userEventIsSelf = userEvents.size() == 1 && + isSelf(userEvent.getDate(), userId, oldEventId, userEvents.get(0)); + + if (isFullDayButDayHasEvents && !userEventIsSelf) { + return new ResponseEntity<>( + "Es gibt bereits Termine am " + userEvent.getDate(), + HttpStatus.BAD_REQUEST + ); + } else { + for (UserEvent ue : userEvents) { + if (ue.getEvent().isFullDay() && !isSelf(userEvent.getDate(), userId, oldEventId, ue)) { return new ResponseEntity<>( "Der Tag " + userEvent.getDate() + " ist schon mit '" + ue.getEvent().getName() + "' belegt", @@ -182,9 +238,15 @@ public class EventController { eventRepository.save(event); userEventRepository.save(userEvent); - }catch (IllegalArgumentException exception){ + } catch (IllegalArgumentException exception) { return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST); } return null; } + + private boolean isSelf(Date date, long userId, long eventId, UserEvent userEvent){ + return date.equals(userEvent.getDate()) && + userId == userEvent.getUser().getId() && + eventId == userEvent.getEvent().getId(); + } } diff --git a/server/src/main/java/com/vpr/server/data/Event.java b/server/src/main/java/com/vpr/server/data/Event.java index bc90f47..0e2ef52 100644 --- a/server/src/main/java/com/vpr/server/data/Event.java +++ b/server/src/main/java/com/vpr/server/data/Event.java @@ -157,11 +157,12 @@ public class Event implements Serializable { @Override public boolean equals(Object obj){ - if(obj.getClass() == Event.class){ + if(!(obj instanceof Event)){ return false; } Event event = (Event) obj; + System.out.println(event.getId() + " " + getId()); return event.getId() == getId(); } diff --git a/server/src/main/java/com/vpr/server/data/User.java b/server/src/main/java/com/vpr/server/data/User.java index 508681f..c3b9da4 100644 --- a/server/src/main/java/com/vpr/server/data/User.java +++ b/server/src/main/java/com/vpr/server/data/User.java @@ -131,7 +131,7 @@ public class User { @Override public boolean equals(Object obj){ - if(obj.getClass() == User.class){ + if(!(obj instanceof User)){ return false; } diff --git a/server/src/main/java/com/vpr/server/data/UserEvent.java b/server/src/main/java/com/vpr/server/data/UserEvent.java index 65e4d20..55c4112 100644 --- a/server/src/main/java/com/vpr/server/data/UserEvent.java +++ b/server/src/main/java/com/vpr/server/data/UserEvent.java @@ -64,11 +64,18 @@ public class UserEvent { @Override public boolean equals(Object obj){ - if(obj.getClass() == UserEvent.class){ + System.out.println("equals"); + if(!(obj instanceof UserEvent)){ + System.out.println("not an userevent"); return false; } UserEvent userEvent = (UserEvent) obj; + + System.out.println("date " + userEvent.getDate().equals(getDate())); + System.out.println("user " + userEvent.getUser().equals(getUser())); + System.out.println("event " + userEvent.getEvent().equals(getEvent())); + return userEvent.getDate().equals(getDate()) && userEvent.getUser().equals(getUser()) && userEvent.getEvent().equals(getEvent());