Project

General

Profile

1
package eu.dnetlib.uoanotificationservice.controllers;
2

    
3
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
4
import eu.dnetlib.uoanotificationservice.entities.Notification;
5
import eu.dnetlib.uoanotificationservice.entities.User;
6
import eu.dnetlib.uoanotificationservice.services.NotificationService;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.security.access.prepost.PreAuthorize;
9
import org.springframework.web.bind.annotation.*;
10

    
11
import java.util.Date;
12
import java.util.List;
13

    
14
@RestController
15
@RequestMapping("/notification")
16
@CrossOrigin(origins = "*")
17
public class NotificationController {
18

    
19
    @Autowired
20
    private NotificationService notificationService;
21

    
22
    @Autowired
23
    private AuthorizationService authorizationService;
24

    
25
    @PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER)")
26
    @RequestMapping(value = {"/all"}, method = RequestMethod.GET)
27
    public List<Notification> getAllNotifications() {
28
        return notificationService.getAllNotifications();
29
    }
30

    
31
    @PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER)")
32
    @RequestMapping(value = {"/{service}"}, method = RequestMethod.GET)
33
    public List<Notification> getMyNotifications(@PathVariable String service) {
34
        return notificationService.getMyNotifications(authorizationService.getAaiId(), authorizationService.getEmail(),
35
                service, authorizationService.getRoles());
36
    }
37

    
38
    @PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER)")
39
    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
40
    public Notification save(@RequestBody Notification notification) {
41
        notification.setUser(authorizationService.getAaiId());
42
        notification.setDate(new Date());
43
        notification.setRead(false);
44
        return notificationService.save(notification);
45
    }
46

    
47
    @PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER) && @Utils.canRead(#id)")
48
    @RequestMapping(value = {"/{id}"}, method = RequestMethod.PUT)
49
    public User readNotification(@PathVariable String id) {
50
        return notificationService.readNotification(authorizationService.getAaiId(), id);
51
    }
52

    
53

    
54
}
55

    
    (1-1/1)