indev
This commit is contained in:
@ -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<List<Notification>> 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<Notification> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import ovh.herisson.Clyde.Tables.Notification;
|
||||
|
||||
interface NotificationRepository extends CrudRepository<Notification, Long> {}
|
||||
public interface NotificationRepository extends CrudRepository<Notification, Long> {}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<Notification> getNotifications(){
|
||||
for(Notification n: this.notifications){
|
||||
if(n.getStatus() == Status.Archived)
|
||||
this.notifications.remove(n);
|
||||
}
|
||||
return this.notifications;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user