Skip to content

Commit

Permalink
Merge pull request #554 from bounswe/feature/BE-552-need-and-resource…
Browse files Browse the repository at this point in the history
…-entity-consistency

Feature/be 552 need and resource entity consistency
  • Loading branch information
furknbulbul authored Dec 12, 2023
2 parents 13b6dc9 + 61296cc commit 369b0bb
Show file tree
Hide file tree
Showing 28 changed files with 325 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.groupa1.resq.controller;


import com.groupa1.resq.request.CreateEventRequest;
import com.groupa1.resq.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.groupa1.resq.response.EventResponse;
import java.util.List;

@RestController
@RequestMapping("/event")
public class EventController {

@Autowired
private EventService eventService;

@PreAuthorize("hasRole('RESPONDER') or hasRole('COORDINATOR') or hasRole('FACILITATOR') or hasRole('VICTIM')")
@PostMapping("/createEvent")
public ResponseEntity<String> createEvent(@RequestBody CreateEventRequest createEventRequest){
return eventService.createEvent(createEventRequest);
}

@PreAuthorize("hasRole('RESPONDER') or hasRole('COORDINATOR') or hasRole('FACILITATOR') or hasRole('VICTIM')")
@GetMapping("/viewEvents")
public ResponseEntity<List<EventResponse>> viewEvents(@RequestParam Long reporterId) {
return eventService.viewEvents( reporterId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public NeedDto convertToDto(Need need) {
needDto.setLatitude(need.getLatitude());
needDto.setLongitude(need.getLongitude());
needDto.setCreatedDate(need.getCreatedAt());
needDto.setSize(need.getSize());
Request request = need.getRequest();
if (request != null) {
needDto.setRequestId(request.getId());
Expand All @@ -51,6 +52,7 @@ public Need convertToEntity(NeedDto needDto) {
need.setLongitude(needDto.getLongitude());
need.setStatus(needDto.getStatus());
need.setCreatedAt(needDto.getCreatedDate());
need.setSize(needDto.getSize());
Request request = requestRepository.findById(needDto.getRequestId()).orElse(null);
need.setRequest(request);
return need;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.groupa1.resq.dto.ResourceDto;
import com.groupa1.resq.entity.Resource;
import com.groupa1.resq.entity.enums.ESize;
import com.groupa1.resq.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +27,7 @@ public ResourceDto convertToDto(Resource resource){
resourceDto.setLatitude(resource.getLatitude());
resourceDto.setLongitude(resource.getLongitude());
resourceDto.setCreatedDate(resource.getCreatedAt());
resourceDto.setSize(resource.getSize().toString());
return resourceDto;

}
Expand All @@ -47,6 +49,7 @@ public Resource convertToEntity(ResourceDto resourceDto){
resource.setLatitude(resourceDto.getLatitude());
resource.setLongitude(resourceDto.getLongitude());
resource.setCreatedAt(resourceDto.getCreatedDate());
resource.setSize(ESize.valueOf(resourceDto.getSize()));
return resource;
}
}
25 changes: 25 additions & 0 deletions resq/backend/resq/src/main/java/com/groupa1/resq/dto/EventDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.groupa1.resq.dto;


import com.groupa1.resq.entity.User;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
public class EventDto {

private Long reporterId;

private String description;

private LocalDateTime reportDate;

private boolean isVerified;

private BigDecimal eventLatitude;
private BigDecimal eventLongitude;


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.groupa1.resq.dto;

import com.groupa1.resq.entity.enums.ENeedStatus;
import com.groupa1.resq.entity.enums.ESize;
import lombok.Data;

import java.math.BigDecimal;
Expand All @@ -18,4 +19,5 @@ public class NeedDto {
private Long requestId;
private ENeedStatus status;
private LocalDateTime createdDate;
private ESize size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class ResourceDto {
private BigDecimal latitude;
private BigDecimal longitude;
private LocalDateTime createdDate;
private String size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class Action extends BaseEntity {
@JoinColumn(name = "verifier_id")
private User verifier;

@Lob
@Column(length = 3000)
@Column(length = 2048)
private String description;

private LocalDateTime dueDate;
Expand Down
38 changes: 38 additions & 0 deletions resq/backend/resq/src/main/java/com/groupa1/resq/entity/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.groupa1.resq.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@NoArgsConstructor
@Entity
@Table( name = "EVENT")
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"reporter"})
@ToString(callSuper = true, exclude = {"reporter"})
public class Event extends BaseEntity {

//@Enumerated(EnumType.STRING)
//private EEventType eventType;

@ManyToOne
@JoinColumn(name = "reporter_id")
private User reporter;

@Column(length = 2048)
private String description;

private LocalDateTime reportDate;

private boolean isVerified;

private BigDecimal eventLatitude;
private BigDecimal eventLongitude;

}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Feedback extends BaseEntity {
@JoinColumn(name = "creator_id")
private User creator;

@Lob
@Column(length = 3000)
@Column(length = 2048)
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class Info extends BaseEntity{
@JoinColumn(name = "user_id")
private User user;

@Lob
@Column(length = 3000)
@Column(length = 2048)
private String description;

private BigDecimal latitude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.groupa1.resq.entity;

import com.groupa1.resq.entity.enums.EGender;
import com.groupa1.resq.entity.enums.ENeedStatus;
import com.groupa1.resq.entity.enums.ESize;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand Down Expand Up @@ -34,6 +36,13 @@ public class Need extends BaseEntity{
private BigDecimal latitude;
private BigDecimal longitude;

@Enumerated(EnumType.STRING)
private EGender gender;

// These field only for clothing
@Enumerated(EnumType.STRING)
private ESize size;

@ManyToOne
@JoinColumn(name = "request_id")
private Request request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class Notification extends BaseEntity{

private String title;

@Lob
@Column(length = 1000)
@Column(length = 2048)
private String body;

private boolean isRead;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.groupa1.resq.entity;

import com.groupa1.resq.entity.enums.EGender;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import com.groupa1.resq.entity.enums.ESize;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -32,6 +30,10 @@ public class Resource extends BaseEntity {

private EGender gender;

// These field only for clothing
@Enumerated(EnumType.STRING)
private ESize size;

private Integer quantity;

private BigDecimal latitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashSet;
Expand Down Expand Up @@ -41,8 +40,7 @@ public class Task extends BaseEntity {
@Enumerated(EnumType.STRING)
private EStatus status;

@Lob
@Column(length = 3000)
@Column(length = 2048)
private String description;

public Task() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@UniqueConstraint(columnNames = "email")
})
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications"})
@ToString(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications"})
@EqualsAndHashCode(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications", "reportedEvents"})
@ToString(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications", "reportedEvents"})
public class User extends BaseEntity {

@NotBlank
Expand All @@ -44,7 +44,8 @@ public class User extends BaseEntity {
@Enumerated(EnumType.STRING)
private Set<EUserRole> roles = new HashSet<>();

@OneToOne(mappedBy = "user")
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_profile_id", referencedColumnName = "id")
private UserProfile userProfile;

@OneToMany(fetch = FetchType.LAZY, mappedBy="requester")
Expand All @@ -68,6 +69,9 @@ public class User extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "assignee")
private Set<Task> tasksAssignedTo;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "reporter")
private Set<Event> reportedEvents;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "creator")
private Set<Feedback> feedbacks;

Expand Down Expand Up @@ -99,5 +103,6 @@ public User() {
this.actions = new HashSet<>();
this.infos = new HashSet<>();
this.notifications = new HashSet<>();
this.reportedEvents = new HashSet<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.groupa1.resq.entity.enums.EGender;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.time.LocalDate;

Expand All @@ -12,6 +14,8 @@
@Entity
@Table( name = "USER_PROFILE")
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"user"})
@ToString(exclude = {"user"})
public class UserProfile extends BaseEntity{

private String name;
Expand All @@ -22,8 +26,7 @@ public class UserProfile extends BaseEntity{
@Enumerated(EnumType.STRING)
private EGender gender;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
@OneToOne(mappedBy = "userProfile")
private User user;

private boolean isEmailConfirmed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.groupa1.resq.entity.enums;

public enum EEventType {
FIRE,
EARTHQUAKE,
FLOOD,
TSUNAMI,
OTHER
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.groupa1.resq.entity.enums;

public enum EGender {
MALE,
FEMALE
MAN,
WOMAN,
CHILDREN_BOY, CHILDREN_GIRL,
BABY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.groupa1.resq.entity.enums;

public enum ESize {
XL, L, M, S, XS, // For Gender: MAN, WOMAN
AGE_0_2, AGE_2_4, // For Gender: BABY
AGE_4_8, AGE_8_12, AGE_12_18, // For Gender: CHILDREN_BOY, CHILDREN_GIRL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.groupa1.resq.repository;


import com.groupa1.resq.entity.Event;
import com.groupa1.resq.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findByReporter(User reporter);

}
Loading

0 comments on commit 369b0bb

Please sign in to comment.