1
0
forked from PGL/Clyde

adding members management

This commit is contained in:
Debucquoy Anthony 2024-03-27 23:54:59 +01:00
parent 1522d74ed3
commit 729d1ad504
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
6 changed files with 77 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package ovh.herisson.Clyde.EndPoints.Msg; package ovh.herisson.Clyde.EndPoints.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; 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;
@ -13,9 +14,11 @@ import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ovh.herisson.Clyde.DTO.Msg.DiscussionDTO; import ovh.herisson.Clyde.DTO.Msg.DiscussionDTO;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; 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.UserService;
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;
@ -29,6 +32,7 @@ public class MessagesController {
private AuthenticatorService authServ; private AuthenticatorService authServ;
private DiscussionService discServ; private DiscussionService discServ;
private DiscussionRepository discRepo; private DiscussionRepository discRepo;
private UserService userServ;
@GetMapping("/discussions") @GetMapping("/discussions")
public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){ public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){
@ -60,6 +64,20 @@ public class MessagesController {
return new ResponseEntity<>(disc, HttpStatus.OK); return new ResponseEntity<>(disc, HttpStatus.OK);
} }
@PatchMapping("/discussion/{id}/add")
public ResponseEntity<Discussion> AlterDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody User data){
User user = authServ.getUserFromToken(token);
if(user == null){
return new UnauthorizedResponse<>(null);
}
Discussion disc = discRepo.findById(id).orElse(null);
User invited = userServ.getUserById(data.getRegNo());
disc.addMember(invited);
discRepo.save(disc);
return new ResponseEntity<>(disc, HttpStatus.OK);
}
@PostMapping("/discussion/{id}") @PostMapping("/discussion/{id}")
public ResponseEntity<Discussion> sendMessage(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Message msg){ public ResponseEntity<Discussion> sendMessage(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Message msg){
User user = authServ.getUserFromToken(token); User user = authServ.getUserFromToken(token);

View File

@ -51,4 +51,8 @@ public class Discussion{
msg.setDiscussion(this); msg.setDiscussion(this);
msgs.add(msg); msgs.add(msg);
} }
public void addMember(User user) {
members.add(user);
}
} }

View File

@ -2,11 +2,13 @@ package ovh.herisson.Clyde.Tables.Msg;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
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;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne; import jakarta.persistence.OneToOne;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@ -26,7 +28,7 @@ public class Message {
private long id; private long id;
private String content; private String content;
@OneToOne @ManyToOne
private User author; private User author;
public User getAuthor() { public User getAuthor() {

View File

@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*; import jakarta.persistence.*;
import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Discussion;
import ovh.herisson.Clyde.Tables.Msg.Message;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -24,6 +25,11 @@ public class User {
private ovh.herisson.Clyde.Tables.Role role; private ovh.herisson.Clyde.Tables.Role role;
private String password; private String password;
////// Extension Messagerie /////
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
private List<Message> msgs;
/////////////////////////////////
@ManyToMany( mappedBy = "members" ) @ManyToMany( mappedBy = "members" )
private List<Discussion> discussions; private List<Discussion> discussions;

View File

@ -7,9 +7,10 @@
<script setup> <script setup>
import { ref, reactive } from 'vue' import { ref, reactive } from 'vue'
import { discussionsList, currentDiscussion, fetchDiscussion, createDiscussion, sendMessage, updateDiscussionName} from '@/rest/msg.js' import { discussionsList, currentDiscussion, fetchDiscussion, createDiscussion, sendMessage, updateDiscussionName, invite} from '@/rest/msg.js'
const msgContent = ref(""); const msgContent = ref("");
const addMember = ref(false);
</script> </script>
@ -19,8 +20,8 @@
<div @click="fetchDiscussion(discussion.id)" class="discItem" v-for="discussion in discussionsList" :key="discussion.id">{{ discussion.name }}</div> <div @click="fetchDiscussion(discussion.id)" class="discItem" v-for="discussion in discussionsList" :key="discussion.id">{{ discussion.name }}</div>
<button id="createDiscussion" @click="createDiscussion('New Discussion')">+</button> <button id="createDiscussion" @click="createDiscussion('New Discussion')">+</button>
</div> </div>
<div id="discussion"> <div id="discussion" v-if="currentDiscussion.length != 0">
<h1 id=msgName ><input class="InputTitle" type="text" @blur="updateDiscussionName(currentDiscussion.id, currentDiscussion.name)" v-model="currentDiscussion.name"></h1> <h1 id=msgName ><input class="InputTitle" type="text" @change="updateDiscussionName(currentDiscussion.id, currentDiscussion.name)" v-model="currentDiscussion.name"></h1>
<div id=msgs> <div id=msgs>
<div class="msg" v-for="msg in currentDiscussion.msgs" :sender="msg.sender" :key="msg.id"> <div class="msg" v-for="msg in currentDiscussion.msgs" :sender="msg.sender" :key="msg.id">
{{ msg.content }} {{ msg.content }}
@ -31,6 +32,10 @@
<input type="submit" @click="sendMessage(currentDiscussion.id, msgContent, null)" value="send"> <input type="submit" @click="sendMessage(currentDiscussion.id, msgContent, null)" value="send">
</div> </div>
</div> </div>
<div id="members" v-if="currentDiscussion.length != 0">
<div class="memberItem" v-for="member in currentDiscussion.members" :key="member.id">{{ member.firstName }} {{ member.lastName.toUpperCase() }}</div>
<input type=text id="addMembers" @focus="addMember = true" @blur="addMember = false;$event.target.value = ''" @change="invite(currentDiscussion.id, $event.target.value)" :placeholder="addMember ? 'Regno' : '+'"/>
</div>
</div> </div>
</template> </template>
@ -42,7 +47,7 @@ div#msg{
height: 100%; height: 100%;
display: grid; display: grid;
grid-template-columns: 20% auto; grid-template-columns: 20% auto 10%;
} }
div#discList{ div#discList{
@ -53,7 +58,17 @@ div#discList{
padding: 10px; padding: 10px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
}
div#members{
margin: 30px 0;
border-radius: 10px 0 0 10px;
background-color: red;
background-color: rgba(255, 255, 255, 0.05);
overflow: hidden;
display: flex;
padding: 10px 0 0 10px;
flex-direction: column;
} }
.InputTitle{ .InputTitle{
@ -74,6 +89,19 @@ div#discList{
border: 1px solid darkorange; border: 1px solid darkorange;
} }
.memberItem{
color: darkorange;
display: flex;
font-family: sans-serif;
font-weight: bold;
height: 4vh;
margin: 5px;
border-radius: 30px 0 0 30px;
align-items: center;
justify-content: center;
border: 1px solid darkorange;
}
#createDiscussion{ #createDiscussion{
height: 4vh; height: 4vh;
margin: 5px; margin: 5px;
@ -84,6 +112,17 @@ div#discList{
font-weight: 900; font-weight: 900;
font-size: 2em; font-size: 2em;
} }
#addMembers{
height: 4vh;
margin: 5px;
text-align: center;
color: white;
background-color: green;
border-radius: 30px 0 0 30px;
border: none;
font-weight: 900;
font-size: 2em;
}
div#discussion{ div#discussion{
display: flex; display: flex;

View File

@ -13,8 +13,8 @@ import { ref } from 'vue'
* - name * - name
* - members * - members
*/ */
export const discussionsList = ref({}); export const discussionsList = ref();
export const currentDiscussion = ref({}); export const currentDiscussion = ref([]);
export async function createDiscussion(name){ export async function createDiscussion(name){
@ -24,7 +24,7 @@ export async function createDiscussion(name){
export async function invite(id, regNo){ export async function invite(id, regNo){
restPost("/discussion/"+ id+ "/invite", {user: regNo}); restPatch("/discussion/"+ id+ "/add", {regNo: parseInt(regNo)}).then(() => fetchDiscussion(id))
} }
export async function sendMessage(id, content, responseId){ export async function sendMessage(id, content, responseId){