Search in sources :

Example 1 with Notification

use of com.arnaugarcia.uplace.domain.Notification in project uplace.es by Uplace.

the class NotificationService method delete.

/*
     * Delete the notification by id.
     *
     * @param id the id of the entity
     */
public void delete(Long id) {
    log.debug("Request to delete Notification : {}", id);
    Notification notification = notificationRepository.findOne(id);
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // If the notification.user does not match and isn't admin... error
    if (!notification.getUser().equals(user) && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        throw new BadRequestAlertException("This notification doesn't belongs to you :)", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_USER);
    }
    notificationRepository.delete(id);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Notification(com.arnaugarcia.uplace.domain.Notification)

Example 2 with Notification

use of com.arnaugarcia.uplace.domain.Notification in project uplace.es by Uplace.

the class NotificationResource method createNotification.

/**
 * POST  /notifications : Create a new notification.
 *
 * @param notification the notification to create
 * @return the ResponseEntity with status 201 (Created) and with body the new notification, or with status 400 (Bad Request) if the notification has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/notifications")
@Timed
public ResponseEntity<Notification> createNotification(@Valid @RequestBody Notification notification) throws URISyntaxException {
    log.debug("REST request to save Notification : {}", notification);
    if (notification.getId() != null) {
        throw new BadRequestAlertException("A new notification cannot already have an ID", ENTITY_NAME, ErrorConstants.ERR_ID_EXISTS);
    }
    Notification result = notificationService.saveNotification(notification);
    return ResponseEntity.created(new URI("/api/notifications/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Notification(com.arnaugarcia.uplace.domain.Notification) Timed(com.codahale.metrics.annotation.Timed)

Example 3 with Notification

use of com.arnaugarcia.uplace.domain.Notification in project uplace.es by Uplace.

the class NotificationService method findOneByType.

/**
 * Get one notification by id.
 *
 * @param id the id of the entity
 * @return the entity
 */
@Transactional(readOnly = true)
public Notification findOneByType(NotificationType notificationType, Long id) {
    log.debug("Request to get Notification : {}", id);
    Notification notification = null;
    switch(notificationType) {
        case NOTIFICATION:
            notification = notificationRepository.findOneByType(NotificationType.NOTIFICATION, id);
            break;
        case REQUEST:
            notification = notificationRepository.findOneByType(NotificationType.REQUEST, id);
            break;
        case ALERT:
            throw new BadRequestAlertException("Not yet implemented", ENTITY_REQUEST, ErrorConstants.NOT_IMPLEMENTED);
    }
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    if (notification == null || !notification.getType().equals(NotificationType.NOTIFICATION)) {
        throw new BadRequestAlertException("No notification was found with this ID", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_ID);
    }
    // If the notification.user does not match and isn't admin... error
    if (!notification.getUser().equals(user) && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        throw new BadRequestAlertException("This notification doesn't belongs to you :)", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_USER);
    }
    return notification;
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Notification(com.arnaugarcia.uplace.domain.Notification) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Notification

use of com.arnaugarcia.uplace.domain.Notification in project uplace.es by Uplace.

the class NotificationResource method deleteNotification.

/**
 * GET  /notifications/read
 * @return the ResponseEntity with status 200 (OK) and with body the notifications updated, or with status 404 (Not Found)
 */
/*@PutMapping("/notifications/read")
    @Timed
    public List<Notification> markNotificationsAsRead(@RequestParam List<Notification> notificationsList) {
        log.debug("REST request to mark all the Notifications as read : {}");

        return notificationService.markAs(notificationsList, true);

    }*/
/**
 * GET  /notifications/unread
 * @return the List<Notification> with status 200 (OK) and with body the notifications updated, or with status 404 (Not Found)
 */
/*@GetMapping("/notifications/unread")
    @Timed
    public List<Notification> markNotificationsAsUnRead(@RequestParam List<Notification> notificationsList) {
        log.debug("REST request to mark all the Notifications as unread : {}");

        return notificationService.markAs(notificationsList, false);
    }*/
/**
 * DELETE  /notifications/:id : delete the "id" notification.
 *
 * @param id the id of the notification to delete
 * @return the ResponseEntity with status 200 (OK)
 */
@DeleteMapping("/notifications/{id}")
@Timed
public ResponseEntity<Void> deleteNotification(@PathVariable Long id) {
    log.debug("REST request to delete Notification : {}", id);
    Notification notification = notificationService.findOneByType(NotificationType.NOTIFICATION, id);
    if (notification != null) {
        notificationService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    } else {
        throw new BadRequestAlertException("The requested notification doesn't exists", ENTITY_NAME, ErrorConstants.ERR_BAD_ID);
    }
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) Notification(com.arnaugarcia.uplace.domain.Notification) Timed(com.codahale.metrics.annotation.Timed)

Example 5 with Notification

use of com.arnaugarcia.uplace.domain.Notification in project uplace.es by Uplace.

the class NotificationResource method getNotification.

/**
 * GET  /notifications/:id : get the "id" notification.
 *
 * @param id the id of the notification to retrieve
 * @return the ResponseEntity with status 200 (OK) and with body the notification, or with status 404 (Not Found)
 */
@GetMapping("/notifications/{id}")
@Timed
public ResponseEntity<Notification> getNotification(@PathVariable Long id) {
    log.debug("REST request to get Notification : {}", id);
    Notification notification = notificationService.findOneByType(NotificationType.NOTIFICATION, id);
    return ResponseUtil.wrapOrNotFound(Optional.ofNullable(notification));
}
Also used : Notification(com.arnaugarcia.uplace.domain.Notification) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Notification (com.arnaugarcia.uplace.domain.Notification)8 BadRequestAlertException (com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException)4 Timed (com.codahale.metrics.annotation.Timed)4 User (com.arnaugarcia.uplace.domain.User)3 Transactional (org.springframework.transaction.annotation.Transactional)2 URI (java.net.URI)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1