Managing the message with backend
Using DTO to change the way discussions and message are sent to the client
This commit is contained in:
parent
914f6bdf36
commit
b4499e04c7
@ -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);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -11,11 +12,14 @@ 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;
|
||||
import ovh.herisson.Clyde.Services.Msg.DiscussionService;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Discussion;
|
||||
import ovh.herisson.Clyde.Tables.Msg.Message;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
|
||||
@ -24,6 +28,7 @@ public class MessagesController {
|
||||
|
||||
private AuthenticatorService authServ;
|
||||
private DiscussionService discServ;
|
||||
private DiscussionRepository discRepo;
|
||||
|
||||
@GetMapping("/discussions")
|
||||
public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){
|
||||
@ -38,10 +43,38 @@ public class MessagesController {
|
||||
}
|
||||
|
||||
@GetMapping("/discussion/{id}")
|
||||
public ResponseEntity<Discussion> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){
|
||||
return null; // TODO
|
||||
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}")
|
||||
public ResponseEntity<Discussion> AlterDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Discussion data){
|
||||
User user = authServ.getUserFromToken(token);
|
||||
if(user == null){
|
||||
return new UnauthorizedResponse<>(null);
|
||||
}
|
||||
|
||||
Discussion disc = discRepo.findById(id).orElse(null);
|
||||
disc.setName(data.getName());
|
||||
discRepo.save(disc);
|
||||
return new ResponseEntity<>(disc, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/discussion/{id}")
|
||||
public ResponseEntity<Discussion> sendMessage(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Message msg){
|
||||
User user = authServ.getUserFromToken(token);
|
||||
if(user == null){
|
||||
return new UnauthorizedResponse<>(null);
|
||||
}
|
||||
|
||||
Discussion disc = discRepo.findById(id).orElse(null);
|
||||
msg.setAuthor(user);
|
||||
if(disc != null)
|
||||
discServ.CreateMessage(disc, msg);
|
||||
return new ResponseEntity<>(disc, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/discussion")
|
||||
public ResponseEntity<Discussion> createDiscussion(@RequestHeader("Authorization") String token, @RequestBody Discussion data){
|
||||
return new ResponseEntity<>(discServ.create(data.getName(), authServ.getUserFromToken(token)), HttpStatus.OK);
|
||||
|
@ -0,0 +1,8 @@
|
||||
package ovh.herisson.Clyde.Repositories.Msg;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import ovh.herisson.Clyde.Tables.Msg.Message;
|
||||
|
||||
public interface MessageRepository extends CrudRepository<Message, Long> {
|
||||
}
|
@ -1,16 +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){
|
||||
@ -21,4 +24,8 @@ public class DiscussionService {
|
||||
return discRepo.findByMembership(author.getRegNo());
|
||||
}
|
||||
|
||||
public Discussion CreateMessage(Discussion disc, Message msg){
|
||||
disc.addMessage(msg);
|
||||
return discRepo.save(disc);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Tables.Msg;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
@ -9,6 +10,7 @@ import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
@ -33,6 +35,9 @@ public class Discussion{
|
||||
)
|
||||
private List<User> members;
|
||||
|
||||
@OneToMany(mappedBy="discussion", orphanRemoval = true, cascade = CascadeType.ALL)
|
||||
private List<Message> msgs;
|
||||
|
||||
public Discussion(String name){
|
||||
this.name = name;
|
||||
}
|
||||
@ -41,4 +46,9 @@ public class Discussion{
|
||||
this.name = name;
|
||||
this.members = List.of(user);
|
||||
}
|
||||
|
||||
public void addMessage(Message msg){
|
||||
msg.setDiscussion(this);
|
||||
msgs.add(msg);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package ovh.herisson.Clyde.Tables.Msg;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Message {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String content;
|
||||
|
||||
@OneToOne
|
||||
private User author;
|
||||
|
||||
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