Fixed equals

This commit is contained in:
Marc Beyer 2022-02-01 15:44:03 +01:00
parent 3b711b5d9f
commit fcfeaf0979
4 changed files with 90 additions and 20 deletions

View File

@ -15,6 +15,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List; import java.util.List;
@Controller @Controller
@ -72,7 +73,7 @@ public class EventController {
} }
eventRepository.deleteUserEventsById(userId, eventId, date); eventRepository.deleteUserEventsById(userId, eventId, date);
if(eventDAO.getAllEventsWithId(eventId).size() == 0){ if (eventDAO.getAllEventsWithId(eventId).size() == 0) {
eventRepository.deleteById(eventId); eventRepository.deleteById(eventId);
} }
@ -115,7 +116,10 @@ public class EventController {
) { ) {
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]); User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) { 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<Event> eventList = eventDAO.getAllEventsWithIdAndDate(userId, eventId, date); List<Event> 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); return new ResponseEntity<>("Der Termin exestiert nicht in der Datenbank", HttpStatus.BAD_REQUEST);
} }
if (eventList.size() > 1) { 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<String> error = createEventAndUserEvent(userId, newDate, newName, newStart, newEnd, newPriority, newIsFullDay, newIsPrivate); ResponseEntity<String> error = createEventAndUserEvent(
userId,
newDate,
newName,
newStart,
newEnd,
newPriority,
newIsFullDay,
newIsPrivate,
eventId
);
if (error != null) return error; if (error != null) return error;
eventRepository.deleteUserEventsById(userId, eventId, date); eventRepository.deleteUserEventsById(userId, eventId, date);
if(eventDAO.getAllEventsWithId(eventId).size() == 0){ if (eventDAO.getAllEventsWithId(eventId).size() == 0) {
eventRepository.deleteById(eventId); eventRepository.deleteById(eventId);
} }
@ -139,9 +158,42 @@ public class EventController {
return new ResponseEntity<>("", HttpStatus.OK); return new ResponseEntity<>("", HttpStatus.OK);
} }
private ResponseEntity<String> createEventAndUserEvent(long userId, String date, String name, String start, String end, Integer priority, Boolean isFullDay, Boolean isPrivate) { private ResponseEntity<String> 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<String> createEventAndUserEvent(
long userId,
String date,
String name,
String start,
String end,
Integer priority,
Boolean isFullDay,
Boolean isPrivate,
long oldEventId
) {
User user = userRepository.findById(userId); User user = userRepository.findById(userId);
if(user == null){ if (user == null) {
return new ResponseEntity<>("UserId nicht korrekt", HttpStatus.BAD_REQUEST); return new ResponseEntity<>("UserId nicht korrekt", HttpStatus.BAD_REQUEST);
} }
@ -162,15 +214,19 @@ public class EventController {
userEvent.setUser(user); userEvent.setUser(user);
List<UserEvent> userEvents = userEventRepository.findByUserIdAndDate(user.getId(), userEvent.getDate()); List<UserEvent> userEvents = userEventRepository.findByUserIdAndDate(user.getId(), userEvent.getDate());
System.out.println(userEvents.size() + "");
if( boolean isFullDayButDayHasEvents = event.isFullDay() && userEvents.size() > 0;
(event.isFullDay() && userEvents.size() > 0) && boolean userEventIsSelf = userEvents.size() == 1 &&
!(userEvents.size() == 1 && userEvent.equals(userEvents.get(0))) isSelf(userEvent.getDate(), userId, oldEventId, userEvents.get(0));
){
return new ResponseEntity<>("Es gibt bereits Termine am " + userEvent.getDate(), HttpStatus.BAD_REQUEST); if (isFullDayButDayHasEvents && !userEventIsSelf) {
}else{ return new ResponseEntity<>(
for(UserEvent ue : userEvents){ "Es gibt bereits Termine am " + userEvent.getDate(),
if(ue.getEvent().isFullDay()){ HttpStatus.BAD_REQUEST
);
} else {
for (UserEvent ue : userEvents) {
if (ue.getEvent().isFullDay() && !isSelf(userEvent.getDate(), userId, oldEventId, ue)) {
return new ResponseEntity<>( return new ResponseEntity<>(
"Der Tag " + userEvent.getDate() + " ist schon mit '" "Der Tag " + userEvent.getDate() + " ist schon mit '"
+ ue.getEvent().getName() + "' belegt", + ue.getEvent().getName() + "' belegt",
@ -182,9 +238,15 @@ public class EventController {
eventRepository.save(event); eventRepository.save(event);
userEventRepository.save(userEvent); userEventRepository.save(userEvent);
}catch (IllegalArgumentException exception){ } catch (IllegalArgumentException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
} }
return null; 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();
}
} }

View File

@ -157,11 +157,12 @@ public class Event implements Serializable {
@Override @Override
public boolean equals(Object obj){ public boolean equals(Object obj){
if(obj.getClass() == Event.class){ if(!(obj instanceof Event)){
return false; return false;
} }
Event event = (Event) obj; Event event = (Event) obj;
System.out.println(event.getId() + " " + getId());
return event.getId() == getId(); return event.getId() == getId();
} }

View File

@ -131,7 +131,7 @@ public class User {
@Override @Override
public boolean equals(Object obj){ public boolean equals(Object obj){
if(obj.getClass() == User.class){ if(!(obj instanceof User)){
return false; return false;
} }

View File

@ -64,11 +64,18 @@ public class UserEvent {
@Override @Override
public boolean equals(Object obj){ 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; return false;
} }
UserEvent userEvent = (UserEvent) obj; 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()) && return userEvent.getDate().equals(getDate()) &&
userEvent.getUser().equals(getUser()) && userEvent.getUser().equals(getUser()) &&
userEvent.getEvent().equals(getEvent()); userEvent.getEvent().equals(getEvent());