From 881b30e5c9de0823496c608204728637e5aa49c5 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 17 Apr 2024 12:00:52 +0200 Subject: [PATCH 1/6] indev --- .../EndPoints/NotificationController.java | 44 +++++++++++ .../herisson/Clyde/Tables/Notification.java | 37 +++++++++ .../java/ovh/herisson/Clyde/Tables/User.java | 77 ++----------------- 3 files changed, 86 insertions(+), 72 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java new file mode 100644 index 0000000..7dac27a --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java @@ -0,0 +1,44 @@ +package ovh.herisson.Clyde.EndPoints; + +import java.util.List; + +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import lombok.AllArgsConstructor; +import ovh.herisson.Clyde.Responses.UnauthorizedResponse; +import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Tables.Notification; +import ovh.herisson.Clyde.Tables.User; + +@RestController +@AllArgsConstructor +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +public class NotificationController { + + private AuthenticatorService authServ; + + @GetMapping("/notifications") + public ResponseEntity> getNotifications(@RequestHeader("Authorization") String token){ + User u = authServ.getUserFromToken(token); + if(u == null){ + return new UnauthorizedResponse<>(null); + } + List n = u.getNotifications(); + return new ResponseEntity<>(n, HttpStatus.OK); + + } + + @PostMapping("/notifications/{id}") + public ResponseStatus archiveNotification(@RequestHeader("Authorization") String token, @PathVariable long id){ + return null; + } + +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java new file mode 100644 index 0000000..36ddaa5 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java @@ -0,0 +1,37 @@ +package ovh.herisson.Clyde.Tables; + +import java.util.Date; + +import org.hibernate.annotations.CreationTimestamp; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import lombok.Data; + +@Data +@Entity +public class Notification { + + private enum Status { + Unread, + Read, + } + + @Id + private int id; + + private String Subject; + + private String body; + + private Status status; + + private String link; + + @ManyToOne + private User user; + + @CreationTimestamp + private Date creation; +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index 2badd32..be615f0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -1,6 +1,7 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import lombok.Data; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; @@ -10,6 +11,7 @@ import java.util.List; @Entity @Table(name = "Users") +@Data public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -25,6 +27,9 @@ public class User { private ovh.herisson.Clyde.Tables.Role role; private String password; + @OneToMany(mappedBy = "user") + private List notifications; + ////// Extension Messagerie ///// @OneToMany(mappedBy = "author", cascade = CascadeType.ALL) private List msgs; @@ -60,76 +65,4 @@ public class User { this.password = password; this.role = Role.Student; } - public User() {} - - public Long getRegNo(){ - return this.regNo; - } - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - public Date getBirthDate() { - return birthDate; - } - - public void setBirthDate(Date birthDate) { - this.birthDate = birthDate; - } - - public String getProfilePictureUrl(){return this.profilePictureUrl;} - - public void setProfilePictureUrl(String profilePictureUrl){ - this.profilePictureUrl = profilePictureUrl; - } - public ovh.herisson.Clyde.Tables.Role getRole() { - return role; - } - - public void setRole(ovh.herisson.Clyde.Tables.Role role) { - this.role = role; - } - public String getPassword(){ - return password; - } - - public void setPassword(String password) { - this.password = password; - } } From 8fa29460ef5c4c408713247e382a4906a37fef65 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 17 Apr 2024 21:41:29 +0200 Subject: [PATCH 2/6] base --- .../Repositories/NotificationRepository.java | 8 ++++++ .../Clyde/Services/Msg/DiscussionService.java | 7 +++++ .../Clyde/Services/NotificationService.java | 9 +++++++ .../herisson/Clyde/Services/UserService.java | 7 +++++ .../herisson/Clyde/Tables/Notification.java | 15 +++++++++-- .../java/ovh/herisson/Clyde/Tables/User.java | 14 +++++++--- frontend/public/i18n/EN.txt | 1 + frontend/public/i18n/FR.txt | 1 + frontend/src/App.vue | 27 ++++++++++++++----- frontend/src/rest/notifications.js | 12 +++++++++ 10 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/NotificationService.java create mode 100644 frontend/src/rest/notifications.js diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java new file mode 100644 index 0000000..2d7ce13 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java @@ -0,0 +1,8 @@ +package ovh.herisson.Clyde.Repositories; + +import org.springframework.data.repository.CrudRepository; + +import ovh.herisson.Clyde.Tables.Notification; + +interface NotificationRepository extends CrudRepository {} + diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java index 9a471da..93703ef 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java @@ -17,6 +17,8 @@ import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.util.JSONPObject; import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; +import ovh.herisson.Clyde.Services.UserService; +import ovh.herisson.Clyde.Tables.Notification; import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; @@ -26,6 +28,8 @@ public class DiscussionService { @Autowired private DiscussionRepository discRepo; + @Autowired + private UserService userServ; public Discussion create(String name, User author){ return discRepo.save(new Discussion(name, author)); @@ -42,6 +46,9 @@ public class DiscussionService { * Create a message and link it to it's discussion */ public Discussion CreateMessage(Discussion disc, Message msg){ + for(User u: disc.getMembers()){ + userServ.Notify(u, new Notification("msg.notification.new", msg.getContent(), "/#/msg")); + } disc.addMessage(msg); return discRepo.save(disc); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/NotificationService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/NotificationService.java new file mode 100644 index 0000000..db0f02e --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/NotificationService.java @@ -0,0 +1,9 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class NotificationService { + +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 72eabd5..e88e565 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -3,6 +3,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.UserRepository; +import ovh.herisson.Clyde.Tables.Notification; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; import java.util.*; @@ -131,4 +132,10 @@ public class UserService { public void delete(User user) { userRepo.delete(user); } + + public void Notify(User u, Notification n){ + n.setUser(u); + u.getNotifications().add(n); + userRepo.save(u); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java index 36ddaa5..ce8f0ad 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java @@ -4,12 +4,17 @@ import java.util.Date; import org.hibernate.annotations.CreationTimestamp; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import jakarta.annotation.Nullable; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@NoArgsConstructor @Entity public class Notification { @@ -21,11 +26,11 @@ public class Notification { @Id private int id; - private String Subject; + private String subject; private String body; - private Status status; + private Status status = Status.Unread; private String link; @@ -34,4 +39,10 @@ public class Notification { @CreationTimestamp private Date creation; + + public Notification(String subject, @Nullable String body, @Nullable String link){ + this.subject = subject; + this.body = body; + this.link = link; + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index be615f0..2e51bb9 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -2,15 +2,19 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; import lombok.Data; +import lombok.NoArgsConstructor; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; import java.util.Date; import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnore; + @Entity @Table(name = "Users") +@NoArgsConstructor @Data public class User { @Id @@ -24,19 +28,23 @@ public class User { private String country; private Date birthDate; private String profilePictureUrl; - private ovh.herisson.Clyde.Tables.Role role; + private Role role; + @JsonIgnore private String password; - @OneToMany(mappedBy = "user") + @JsonIgnore + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) private List notifications; ////// Extension Messagerie ///// + @JsonIgnore @OneToMany(mappedBy = "author", cascade = CascadeType.ALL) private List msgs; - ///////////////////////////////// + @JsonIgnore @ManyToMany( mappedBy = "members" ) private List discussions; + ///////////////////////////////// public User(String lastName, String firstName, String email, String address, String country, Date birthDate, String profilePictureUrl, Role role, String password) diff --git a/frontend/public/i18n/EN.txt b/frontend/public/i18n/EN.txt index e79a8a1..ae5a660 100644 --- a/frontend/public/i18n/EN.txt +++ b/frontend/public/i18n/EN.txt @@ -52,3 +52,4 @@ Curriculum=curriculum Credits=Credits InscriptionService=I.S. faculty=Faculty +msg.notification.new=You have a new message diff --git a/frontend/public/i18n/FR.txt b/frontend/public/i18n/FR.txt index c5f3ebf..8fcfe72 100644 --- a/frontend/public/i18n/FR.txt +++ b/frontend/public/i18n/FR.txt @@ -52,3 +52,4 @@ Curriculum=Cursus Credits=Credits InscriptionService=S.I. faculty=Faculté +msg.notification.new=Vous avez un nouveau message! diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 6dcad67..82124fa 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -3,6 +3,7 @@ import { ref } from 'vue' import i18n, { setLang } from './i18n.js' import { isLogged } from '@/rest/Users.js' + import { notifications, fetchNotifications, archiveNotification } from '@/rest/notifications.js' import { appList, currentView } from '@/rest/apps.js' var prevURL; @@ -14,16 +15,20 @@ window.onhashchange = function() { } const Logged = ref(isLogged()); +if(Logged){ + fetchNotifications(); +} + window.addEventListener('hashchange', () => { if((location.hash === "#/home" && prevURL === "#/login") || (location.hash === "#/home" && prevURL === "#/profil")){ window.location.reload(); } }); const home=ref(i18n("app.home")) - const notifications=ref(i18n("app.notifications")) const settings=ref(i18n("app.settings")) const login=ref(i18n("app.login")) const active=ref(false) + const notification = ref(false) const apps = ref([]) @@ -46,11 +51,14 @@ window.addEventListener('hashchange', () => {
  • -
    +
  • - -
    +
    +
    +
      +
    • {{ i18n(notif.subject) }} - {{ notif.body }}
    • +
  • @@ -219,8 +227,6 @@ window.addEventListener('hashchange', () => { background-color: black; border-radius:6px; color:white; - transform: translate(0px ,1px); - } ul.vertical:hover { @@ -252,6 +258,15 @@ window.addEventListener('hashchange', () => { .clyde:hover{ content: url("./assets/angry_clyde.png") } + + #notification{ + position: absolute; + top: 61px; + right: 0; + background-color: white; + width: 300px; + height: 600px; + } diff --git a/frontend/src/rest/notifications.js b/frontend/src/rest/notifications.js new file mode 100644 index 0000000..d2b0ccb --- /dev/null +++ b/frontend/src/rest/notifications.js @@ -0,0 +1,12 @@ +import { ref } from 'vue' +import { restGet, restPost } from '@/rest/restConsumer.js' + +export const notifications = ref({}); + +export function fetchNotifications(){ + restGet("/notifications").then( e => notifications.value = e ); +} + +export function archiveNotification(id){ + restPost("/notifications/" + id).then( e => fetchNotifications() ); +} From dff225b0f590b82db092bf5c9b4c5f55028c12f0 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Thu, 18 Apr 2024 08:32:06 +0200 Subject: [PATCH 3/6] Removing toaster at every request It is possible to explicitly request a toaster for a request by setting its config['toast'] to true --- frontend/src/rest/restConsumer.js | 33 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/frontend/src/rest/restConsumer.js b/frontend/src/rest/restConsumer.js index 1af979e..7fce929 100644 --- a/frontend/src/rest/restConsumer.js +++ b/frontend/src/rest/restConsumer.js @@ -3,25 +3,25 @@ import { toast } from 'vue3-toastify' const restURL = import.meta.env.VITE_CLYDE_MODE === 'container' ? "http://localhost:8080": import.meta.env.DEV ? "http://localhost:8080" : "https://clyde.herisson.ovh/api" -export async function restGet(endPoint) { - return await _rest(endPoint, {method: "GET"}); +export function restGet(endPoint) { + return _rest(endPoint, {method: "GET"}); } -export async function restPost(endPoint, data) { - return await _rest(endPoint, {method: "POST", credentials: 'include', body: JSON.stringify(data)}); +export function restPost(endPoint, data) { + return _rest(endPoint, {method: "POST", credentials: 'include', body: JSON.stringify(data)}); } -export async function restPostFile(endPoint, file){ +export function restPostFile(endPoint, file){ let headers = new Headers(); - return await _rest(endPoint, {method: "POST", credentials: 'include', body: file, headers: headers }); + return _rest(endPoint, {method: "POST", credentials: 'include', body: file, headers: headers }); } -export async function restDelete(endPoint) { - return await _rest(endPoint, {method: "DELETE"}); +export function restDelete(endPoint) { + return _rest(endPoint, {method: "DELETE"}); } -export async function restPatch(endPoint, data) { - return await _rest(endPoint, {method: "PATCH", credentials: 'include', body: JSON.stringify(data)}); +export function restPatch(endPoint, data) { + return _rest(endPoint, {method: "PATCH", credentials: 'include', body: JSON.stringify(data)}); } /** @@ -33,7 +33,7 @@ export async function restPatch(endPoint, data) { * * @Example _rest("/ping", {user: data}) -> {id:0, txt:"pong"} */ -async function _rest(endPoint, config){ +function _rest(endPoint, config){ endPoint.at(0) != "/" ? console.error("Carefull, you certainly should put a / at the begenning of your endPoint ") : true; let session_token = getCookie("session_token"); let headers = new Headers({ @@ -41,13 +41,16 @@ async function _rest(endPoint, config){ 'Content-Type': 'application/json', }); config['headers'] = config['headers'] == null ? headers : config['headers']; - return toast.promise(fetch(restURL + endPoint, config), + + let ret = fetch(restURL + endPoint, config); + if(config['toast']){ + ret = toast.promise(ret, { pending: config['pending'] != null ? config['pending'] : 'pending', error: config['error'] != null ? config['error'] : 'Network Failure...', success: config['success'] != null ? config['success'] : {render(res){ return res.data.ok ? "Success" : "error"; - }}, - }) - .then( e => e.json()).catch( e => e ); + }}}) + } + return ret.then( e => e.json()).catch( e => e ); } From f14d41f04dd7a5cd9ae4e6ea5c9010bb496b118e Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 21 Apr 2024 17:42:29 +0200 Subject: [PATCH 4/6] =?UTF-8?q?je=20sais=20vraiment=20pas=20ce=20que=20j'a?= =?UTF-8?q?i=20ajout=C3=A9=20mais=20am=C3=A9lioration=20jt'e=20jure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java | 2 +- backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java | 3 ++- backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java | 3 +++ frontend/src/App.vue | 2 +- frontend/src/Apps/Forums.vue | 2 +- frontend/src/rest/courses.js | 2 -- frontend/src/rest/notifications.js | 2 +- 7 files changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java index a0c74f5..d14265d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java @@ -80,7 +80,7 @@ public class ForumController { public ResponseEntity postTopicToForum(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Topic data){ User u = authServ.getUserFromToken(token); Forum f = forumRepo.findById(id).orElse(null); - if(!(f.getWriters().contains(u) || u.getRole() == Role.Admin)){ + if(!(f.getWriters().contains(u) || f.getCourse().getOwner().equals(u) || u.getRole() == Role.Admin)){ return new UnauthorizedResponse<>(null); } forumServ.createTopic(f, data); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index 369a8bc..1093052 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -28,10 +28,11 @@ public class Course { private User owner; //// Extension Messagerie ///// - @OneToMany(cascade = CascadeType.ALL) + @OneToMany(mappedBy = "course", cascade = CascadeType.ALL) private List forums; public void addForum(Forum f){ + f.setCourse(this); forums.add(f); } /////////////////////////////// diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java index 75508aa..e03d0de 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java @@ -2,6 +2,8 @@ package ovh.herisson.Clyde.Tables.Msg; import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnore; + import jakarta.persistence.*; import lombok.Data; import ovh.herisson.Clyde.Tables.Course; @@ -16,6 +18,7 @@ public class Forum { private int id; @ManyToOne + @JsonIgnore private Course course; private String name; diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ae9caf6..2a15677 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -15,7 +15,7 @@ window.onhashchange = function() { } const Logged = ref(isLogged()); -if(Logged){ +if(Logged.value){ fetchNotifications(); } diff --git a/frontend/src/Apps/Forums.vue b/frontend/src/Apps/Forums.vue index d2004c5..2e7dd49 100644 --- a/frontend/src/Apps/Forums.vue +++ b/frontend/src/Apps/Forums.vue @@ -15,7 +15,7 @@ import { fetchedPost, fetchPost, sendAnswer } from '@/rest/forum.js' import { getSelf } from '@/rest/Users.js' const Role = (await getSelf()).role; -const courses = Role === 'Admin' || Role === 'Secretary' ? await reactive(getCourses()) : await reactive(getUserActualCourses()); +const courses = Role === 'Admin' || Role === 'Secretary' || Role === 'Teacher' ? await reactive(getCourses(Role)) : await reactive(getUserActualCourses()); const selectedCourse = ref(); const selectedForum = ref(); diff --git a/frontend/src/rest/courses.js b/frontend/src/rest/courses.js index 92677e9..3a87f5c 100644 --- a/frontend/src/rest/courses.js +++ b/frontend/src/rest/courses.js @@ -8,8 +8,6 @@ import { restGet, restPost, restDelete, restPatch } from './restConsumer.js' * Create a new course */ export async function createCourse(name, credits, owner){ - console.log(owner); - return restPost("/course", {title: name, credits: credits, owner} ) } diff --git a/frontend/src/rest/notifications.js b/frontend/src/rest/notifications.js index d2b0ccb..31d0319 100644 --- a/frontend/src/rest/notifications.js +++ b/frontend/src/rest/notifications.js @@ -1,7 +1,7 @@ import { ref } from 'vue' import { restGet, restPost } from '@/rest/restConsumer.js' -export const notifications = ref({}); +export const notifications = ref([]); export function fetchNotifications(){ restGet("/notifications").then( e => notifications.value = e ); From 0b9227a82236d3227f4f8848fc81ee7c6107af60 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Fri, 19 Apr 2024 20:22:31 +0200 Subject: [PATCH 5/6] indev --- .../EndPoints/NotificationController.java | 14 +++++++--- .../Repositories/NotificationRepository.java | 2 +- .../herisson/Clyde/Tables/Notification.java | 6 ++++- .../java/ovh/herisson/Clyde/Tables/User.java | 9 +++++++ frontend/src/App.vue | 27 +++++++++++++++---- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java index 7dac27a..536f76b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java @@ -13,10 +13,12 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; +import ovh.herisson.Clyde.Repositories.NotificationRepository; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Tables.Notification; import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Notification.Status; @RestController @AllArgsConstructor @@ -24,6 +26,7 @@ import ovh.herisson.Clyde.Tables.User; public class NotificationController { private AuthenticatorService authServ; + private NotificationRepository notifRepo; @GetMapping("/notifications") public ResponseEntity> getNotifications(@RequestHeader("Authorization") String token){ @@ -37,8 +40,13 @@ public class NotificationController { } @PostMapping("/notifications/{id}") - public ResponseStatus archiveNotification(@RequestHeader("Authorization") String token, @PathVariable long id){ - return null; + public ResponseEntity archiveNotification(@RequestHeader("Authorization") String token, @PathVariable long id){ + User u = authServ.getUserFromToken(token); + Notification n = notifRepo.findById(id).orElse(null); + if(u == null || n.getUser() != u){ + return new UnauthorizedResponse<>(null); + } + n.setStatus(Status.Archived); + return new ResponseEntity<>(HttpStatus.OK); } - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java index 2d7ce13..a9f9546 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java @@ -4,5 +4,5 @@ import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.Notification; -interface NotificationRepository extends CrudRepository {} +public interface NotificationRepository extends CrudRepository {} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java index ce8f0ad..1b7ab53 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.annotation.Nullable; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import lombok.Data; @@ -18,12 +20,14 @@ import lombok.NoArgsConstructor; @Entity public class Notification { - private enum Status { + public enum Status { Unread, Read, + Archived } @Id + @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String subject; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index 4ce2bb4..e2a0c27 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -9,6 +9,7 @@ import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.GenericGenerator; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; +import ovh.herisson.Clyde.Tables.Notification.Status; import java.util.Date; import java.util.List; @@ -77,4 +78,12 @@ public class User { this.password = password; this.role = Role.Student; } + + public List getNotifications(){ + for(Notification n: this.notifications){ + if(n.getStatus() == Status.Archived) + this.notifications.remove(n); + } + return this.notifications; + } } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 2a15677..dfa5ca9 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -49,17 +49,17 @@ window.addEventListener('hashchange', () => {
  • -
    +
  • -
  • - +
  • +
      -
    • {{ i18n(notif.subject) }} - {{ notif.body }}
    • +
    • {{ i18n(notif.subject) }} - {{ notif.body }}
  • - +