DTO
This commit is contained in:
parent
4848a2c269
commit
384bd106be
@ -0,0 +1,26 @@
|
||||
package ovh.herisson.Clyde.DTO.Msg;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Discussion;
|
||||
import ovh.herisson.Clyde.DTO.Msg.MessagesDTO;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DiscussionDTO {
|
||||
private long id;
|
||||
private String name;
|
||||
private List<User> members;
|
||||
private List<MessagesDTO> msgs;
|
||||
|
||||
public static DiscussionDTO construct(Discussion d, User u){
|
||||
List<MessagesDTO> msgsdto = new ArrayList<>();
|
||||
d.getMsgs().forEach(x -> msgsdto.add(MessagesDTO.construct(x, u)));
|
||||
return new DiscussionDTO(d.getId(), d.getName(), d.getMembers(), msgsdto);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ovh.herisson.Clyde.DTO.Msg;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Message;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessagesDTO {
|
||||
private long id;
|
||||
private String content;
|
||||
private User author;
|
||||
private boolean sender;
|
||||
//TODO: Attachment
|
||||
|
||||
public static MessagesDTO construct(Message m, User user){
|
||||
boolean sender = false;
|
||||
if(m.getAuthor().equals(user))
|
||||
sender = true;
|
||||
return new MessagesDTO(m.getId(), m.getContent(), m.getAuthor(), sender);
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import ovh.herisson.Clyde.DTO.Msg.DiscussionDTO;
|
||||
import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository;
|
||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
||||
import ovh.herisson.Clyde.Services.AuthenticatorService;
|
||||
@ -42,8 +43,8 @@ public class MessagesController {
|
||||
}
|
||||
|
||||
@GetMapping("/discussion/{id}")
|
||||
public ResponseEntity<Discussion> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){
|
||||
return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK);
|
||||
public ResponseEntity<DiscussionDTO> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){
|
||||
return new ResponseEntity<>(DiscussionDTO.construct(discRepo.findById(id).orElse(null), authServ.getUserFromToken(token)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PatchMapping("/discussion/{id}")
|
||||
|
@ -1,17 +1,19 @@
|
||||
package ovh.herisson.Clyde.Services.Msg;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import com.fasterxml.jackson.databind.util.JSONPObject;
|
||||
|
||||
import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Discussion;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Message;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class DiscussionService {
|
||||
|
||||
@Autowired
|
||||
private DiscussionRepository discRepo;
|
||||
|
||||
public Discussion create(String name, User author){
|
||||
@ -26,5 +28,4 @@ public class DiscussionService {
|
||||
disc.addMessage(msg);
|
||||
return discRepo.save(disc);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ovh.herisson.Clyde.Tables.Msg;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
@ -27,10 +29,16 @@ public class Message {
|
||||
@OneToOne
|
||||
private User author;
|
||||
|
||||
@OneToOne
|
||||
public User getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@OneToOne
|
||||
private Message response;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
@JsonIgnore
|
||||
private Discussion discussion;
|
||||
|
||||
//TODO: Attachment
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user