Added equals methods

This commit is contained in:
Marc Beyer 2022-02-01 14:05:58 +01:00
parent 5066afbb92
commit 3b711b5d9f
4 changed files with 66 additions and 2 deletions

View File

@ -163,7 +163,10 @@ public class EventController {
List<UserEvent> 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){

View File

@ -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();
}
}

View File

@ -128,4 +128,19 @@ public class User {
public void setEventList(List<UserEvent> 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();
}
}

View File

@ -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 +
'}';
}
}
@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;
}
}