From 3b711b5d9fdaa6a01e582ca9bde5d6b4defc5531 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Tue, 1 Feb 2022 14:05:58 +0100 Subject: [PATCH] Added equals methods --- .../server/controller/EventController.java | 5 ++- .../main/java/com/vpr/server/data/Event.java | 15 +++++++++ .../main/java/com/vpr/server/data/User.java | 15 +++++++++ .../java/com/vpr/server/data/UserEvent.java | 33 ++++++++++++++++++- 4 files changed, 66 insertions(+), 2 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 0421abc..f8d709b 100644 --- a/server/src/main/java/com/vpr/server/controller/EventController.java +++ b/server/src/main/java/com/vpr/server/controller/EventController.java @@ -163,7 +163,10 @@ public class EventController { List userEvents = userEventRepository.findByUserIdAndDate(user.getId(), userEvent.getDate()); System.out.println(userEvents.size() + ""); - if(event.isFullDay() && userEvents.size() > 0){ + 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){ 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 b9eaea3..bc90f47 100644 --- a/server/src/main/java/com/vpr/server/data/Event.java +++ b/server/src/main/java/com/vpr/server/data/Event.java @@ -154,4 +154,19 @@ public class Event implements Serializable { ", userEvent=" + userEvent + '}'; } + + @Override + public boolean equals(Object obj){ + if(obj.getClass() == Event.class){ + return false; + } + + Event event = (Event) obj; + return event.getId() == getId(); + } + + @Override + public int hashCode(){ + return (int)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 b444d38..508681f 100644 --- a/server/src/main/java/com/vpr/server/data/User.java +++ b/server/src/main/java/com/vpr/server/data/User.java @@ -128,4 +128,19 @@ public class User { public void setEventList(List userEvent) { this.userEvent = userEvent; } + + @Override + public boolean equals(Object obj){ + if(obj.getClass() == User.class){ + return false; + } + + User user = (User) obj; + return user.getId() == getId(); + } + + @Override + public int hashCode(){ + return (int)getId(); + } } \ No newline at end of file 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 cbf2859..65e4d20 100644 --- a/server/src/main/java/com/vpr/server/data/UserEvent.java +++ b/server/src/main/java/com/vpr/server/data/UserEvent.java @@ -2,6 +2,7 @@ package com.vpr.server.data; import javax.persistence.*; import java.sql.Date; +import java.util.Calendar; // @Entity creates a table out of this class with Hibernate // @Table defines the table-name @@ -60,4 +61,34 @@ public class UserEvent { ", date=" + date + '}'; } -} \ No newline at end of file + + @Override + public boolean equals(Object obj){ + if(obj.getClass() == UserEvent.class){ + return false; + } + + UserEvent userEvent = (UserEvent) obj; + return userEvent.getDate().equals(getDate()) && + userEvent.getUser().equals(getUser()) && + userEvent.getEvent().equals(getEvent()); + } + + @Override + public int hashCode(){ + long hash = getUser().hashCode() + + getEvent().hashCode() + + getDate().hashCode(); + + return (int)hash; + } +} + + + + + + + + +