This commit is contained in:
Debucquoy Anthony 2024-03-25 13:19:27 +01:00
parent 914f6bdf36
commit 4848a2c269
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
7 changed files with 142 additions and 53 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -11,11 +12,13 @@ import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.Msg.DiscussionService; import ovh.herisson.Clyde.Services.Msg.DiscussionService;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Discussion;
import ovh.herisson.Clyde.Tables.Msg.Message;
@RestController @RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true") @CrossOrigin(originPatterns = "*", allowCredentials = "true")
@ -24,6 +27,7 @@ public class MessagesController {
private AuthenticatorService authServ; private AuthenticatorService authServ;
private DiscussionService discServ; private DiscussionService discServ;
private DiscussionRepository discRepo;
@GetMapping("/discussions") @GetMapping("/discussions")
public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){ public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){
@ -39,9 +43,23 @@ public class MessagesController {
@GetMapping("/discussion/{id}") @GetMapping("/discussion/{id}")
public ResponseEntity<Discussion> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ public ResponseEntity<Discussion> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){
return null; // TODO return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK);
} }
@PatchMapping("/discussion/{id}")
public ResponseEntity<Discussion> AlterDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Discussion data){
return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK);
}
@PostMapping("/discussion/{id}")
public ResponseEntity<Discussion> sendMessage(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Message msg){
Discussion disc = discRepo.findById(id).orElse(null);
if(disc != null)
discServ.CreateMessage(disc, msg);
return new ResponseEntity<>(disc, HttpStatus.OK);
}
@PostMapping("/discussion") @PostMapping("/discussion")
public ResponseEntity<Discussion> createDiscussion(@RequestHeader("Authorization") String token, @RequestBody Discussion data){ public ResponseEntity<Discussion> createDiscussion(@RequestHeader("Authorization") String token, @RequestBody Discussion data){
return new ResponseEntity<>(discServ.create(data.getName(), authServ.getUserFromToken(token)), HttpStatus.OK); return new ResponseEntity<>(discServ.create(data.getName(), authServ.getUserFromToken(token)), HttpStatus.OK);

View File

@ -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> {
}

View File

@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Discussion;
import ovh.herisson.Clyde.Tables.Msg.Message;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
@ -21,4 +22,9 @@ public class DiscussionService {
return discRepo.findByMembership(author.getRegNo()); return discRepo.findByMembership(author.getRegNo());
} }
public Discussion CreateMessage(Discussion disc, Message msg){
disc.addMessage(msg);
return discRepo.save(disc);
}
} }

View File

@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Tables.Msg;
import java.util.List; import java.util.List;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
@ -9,6 +10,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable; import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -33,6 +35,9 @@ public class Discussion{
) )
private List<User> members; private List<User> members;
@OneToMany(mappedBy="discussion", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Message> msgs;
public Discussion(String name){ public Discussion(String name){
this.name = name; this.name = name;
} }
@ -41,4 +46,9 @@ public class Discussion{
this.name = name; this.name = name;
this.members = List.of(user); this.members = List.of(user);
} }
public void addMessage(Message msg){
msg.setDiscussion(this);
msgs.add(msg);
}
} }

View File

@ -0,0 +1,36 @@
package ovh.herisson.Clyde.Tables.Msg;
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;
@OneToOne
private Message response;
@ManyToOne(optional = false)
private Discussion discussion;
}

View File

@ -7,9 +7,10 @@
<script setup> <script setup>
import { ref, reactive } from 'vue' import { ref, reactive } from 'vue'
import { getDiscussions, currentDiscussion, fetchDiscussion, createDiscussion } from '@/rest/msg.js' import { getDiscussions, currentDiscussion, fetchDiscussion, createDiscussion, sendMessage} from '@/rest/msg.js'
const discussionsList = reactive(await getDiscussions()); const discussionsList = reactive(await getDiscussions());
const msgContent = ref("");
</script> </script>
@ -27,8 +28,8 @@
</div> </div>
</div> </div>
<div id=messageBox> <div id=messageBox>
<input type="text" name="messageBox" value=""> <input type="text" v-model="msgContent">
<input type="submit" name="" id="" value="send"> <input type="submit" @click="sendMessage(currentDiscussion.id, msgContent, null)" value="send">
</div> </div>
</div> </div>
</div> </div>

View File

@ -21,55 +21,55 @@ export async function getDiscussions(){
} }
export async function fetchDiscussion(id){ export async function fetchDiscussion(id){
currentDiscussion.value = { // currentDiscussion.value = {
id: id, // id: id,
name: "Discussion#2", // name: "Discussion#2",
msgs: [ // msgs: [
{ // {
id: 1, // id: 1,
author: 1, // author: 1,
sender: true, // sender: true,
attachment: null, // attachment: null,
text: "Hello world!" // text: "Hello world!"
}, // },
{ // {
id: 2, // id: 2,
author: 2, // author: 2,
sender: false, // sender: false,
attachment: null, // attachment: null,
text: "Hello What?" // text: "Hello What?"
}, // },
{ // {
id: 3, // id: 3,
author: 2, // author: 2,
sender: false, // sender: false,
attachment: null, // attachment: null,
text: "You morron" // text: "You morron"
}, // },
{ // {
id: 4, // id: 4,
author: 1, // author: 1,
sender: true, // sender: true,
attachment: null, // attachment: null,
text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
}, // },
// { // // {
// id: 5, // // id: 5,
// author: 1, // // author: 1,
// sender: true, // // sender: true,
// attachment: null, // // attachment: null,
// text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." // // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
// }, // // },
{ // {
id: 6, // id: 6,
author: 2, // author: 2,
sender: false, // sender: false,
attachment: null, // attachment: null,
text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
} // }
] // ]
} // }
// currentDiscussion.value = restGet("/discussion/" + id); currentDiscussion.value = await restGet("/discussion/" + id);
} }
export async function createDiscussion(name){ export async function createDiscussion(name){
@ -80,3 +80,13 @@ export async function createDiscussion(name){
export async function invite(id, regNo){ export async function invite(id, regNo){
restPost("/discussion/"+ id+ "/invite", {user: regNo}); restPost("/discussion/"+ id+ "/invite", {user: regNo});
} }
export async function sendMessage(id, content, responseId){
let data = {
content: content,
response: responseId,
}
console.log(content)
console.log(data)
restPost("/discussion/" + id, data);
}