Skip to content

Commit

Permalink
[#11572] Notification Feature - PUT notification route (#11665)
Browse files Browse the repository at this point in the history
* Add PUT route

* Add PUT to notification service

* Change startTime and endTime to long
  • Loading branch information
NicolasCwy authored and jianhandev committed Apr 6, 2022
1 parent 1f77224 commit 7756f04
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/main/java/teammates/logic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public NotificationAttributes createNotification(NotificationAttributes notifica
return notificationsLogic.createNotification(notification);
}

public NotificationAttributes updateNotification(NotificationAttributes.UpdateOptions updateOptions) throws
InvalidParametersException, EntityDoesNotExistException {
return notificationsLogic.updateNotification(updateOptions);
}

/**
* Deletes notification by ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import teammates.common.datatransfer.NotificationType;

/**
* The request for creating a notification.
* The basic request for a notification.
*/
public class CreateNotificationRequest extends BasicRequest {
private Long startTimestamp;
private Long endTimestamp;
public class NotificationBasicRequest extends BasicRequest {
private long startTimestamp;
private long endTimestamp;
private NotificationType notificationType;
private NotificationTargetUser targetUser;
private String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package teammates.ui.request;

/**
* The request for creating a notification.
*/
public class NotificationCreateRequest extends NotificationBasicRequest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package teammates.ui.request;

/**
* The request for updating a notification.
*/
public class NotificationUpdateRequest extends NotificationBasicRequest {
}
1 change: 1 addition & 0 deletions src/main/java/teammates/ui/webapi/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public final class ActionFactory {
// NOTIFICATION APIs
map(ResourceURIs.NOTIFICATION, GET, GetNotificationAction.class);
map(ResourceURIs.NOTIFICATION, POST, CreateNotificationAction.class);
map(ResourceURIs.NOTIFICATION, PUT, UpdateNotificationAction.class);
map(ResourceURIs.NOTIFICATION, DELETE, DeleteNotificationAction.class);

// NOTIFICATIONS APIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Logger;
import teammates.ui.output.NotificationData;
import teammates.ui.request.CreateNotificationRequest;
import teammates.ui.request.InvalidHttpRequestBodyException;
import teammates.ui.request.NotificationCreateRequest;

/**
* Action: Creates a new notification banner.
Expand All @@ -21,7 +21,7 @@ public class CreateNotificationAction extends AdminOnlyAction {

@Override
public JsonResult execute() throws InvalidHttpRequestBodyException, InvalidOperationException {
CreateNotificationRequest notificationRequest = getAndValidateRequestBody(CreateNotificationRequest.class);
NotificationCreateRequest notificationRequest = getAndValidateRequestBody(NotificationCreateRequest.class);

Instant startTime = Instant.ofEpochMilli(notificationRequest.getStartTimestamp());
Instant endTime = Instant.ofEpochMilli(notificationRequest.getEndTimestamp());
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/teammates/ui/webapi/UpdateNotificationAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package teammates.ui.webapi;

import java.time.Instant;

import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.common.datatransfer.attributes.NotificationAttributes.UpdateOptions;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.ui.output.NotificationData;
import teammates.ui.request.InvalidHttpRequestBodyException;
import teammates.ui.request.NotificationUpdateRequest;

/**
* Action: Updates a new notification banner.
*/
public class UpdateNotificationAction extends AdminOnlyAction {

@Override
public JsonResult execute() throws InvalidHttpRequestBodyException {
String notificationId = getNonNullRequestParamValue(Const.ParamsNames.NOTIFICATION_ID);
NotificationUpdateRequest notificationRequest = getAndValidateRequestBody(NotificationUpdateRequest.class);

Instant startTime = Instant.ofEpochMilli(notificationRequest.getStartTimestamp());
Instant endTime = Instant.ofEpochMilli(notificationRequest.getEndTimestamp());

UpdateOptions newNotification = NotificationAttributes.updateOptionsBuilder(notificationId)
.withStartTime(startTime)
.withEndTime(endTime)
.withType(notificationRequest.getNotificationType())
.withTargetUser(notificationRequest.getTargetUser())
.withTitle(notificationRequest.getTitle())
.withMessage(notificationRequest.getMessage())
.build();

try {
return new JsonResult(new NotificationData(logic.updateNotification(newNotification)));
} catch (InvalidParametersException e) {
throw new InvalidHttpRequestBodyException(e);
} catch (EntityDoesNotExistException ednee) {
throw new EntityNotFoundException(ednee);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.common.util.Const;
import teammates.ui.output.NotificationData;
import teammates.ui.request.CreateNotificationRequest;
import teammates.ui.request.InvalidHttpRequestBodyException;
import teammates.ui.request.NotificationCreateRequest;

/**
* SUT: {@link CreateNotificationAction}.
Expand Down Expand Up @@ -37,7 +37,7 @@ protected void testExecute() throws Exception {

loginAsAdmin();
______TS("Typical Case: Add notification successfully");
CreateNotificationRequest req = getTypicalCreateRequest();
NotificationCreateRequest req = getTypicalCreateRequest();
CreateNotificationAction action = getAction(req);
NotificationData res = (NotificationData) action.execute().getOutput();

Expand Down Expand Up @@ -102,8 +102,8 @@ protected void testAccessControl() throws Exception {
verifyOnlyAdminCanAccess();
}

private CreateNotificationRequest getTypicalCreateRequest() {
CreateNotificationRequest req = new CreateNotificationRequest();
private NotificationCreateRequest getTypicalCreateRequest() {
NotificationCreateRequest req = new NotificationCreateRequest();

req.setStartTimestamp(testNotificationAttribute.getStartTime().toEpochMilli());
req.setEndTimestamp(testNotificationAttribute.getEndTime().toEpochMilli());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ protected void testExecute() {
GetUsageStatisticsAction.class,
GetNotificationAction.class,
CreateNotificationAction.class,
UpdateNotificationAction.class,
DeleteNotificationAction.class,
GetNotificationsAction.class
);
Expand Down
14 changes: 12 additions & 2 deletions src/web/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ResourceEndpoints } from '../types/api-const';
import { MessageOutput, Notification, Notifications } from '../types/api-output';
import { CreateNotificationRequest } from '../types/api-request';
import { NotificationCreateRequest, NotificationUpdateRequest } from '../types/api-request';
import { HttpRequestService } from './http-request.service';

/**
Expand All @@ -18,7 +18,7 @@ export class NotificationService {
/**
* Creates a notification by calling API.
*/
createNotification(request: CreateNotificationRequest): Observable<Notification> {
createNotification(request: NotificationCreateRequest): Observable<Notification> {
return this.httpRequestService.post(ResourceEndpoints.NOTIFICATION, {}, request);
}

Expand All @@ -29,6 +29,16 @@ export class NotificationService {
return this.httpRequestService.get(ResourceEndpoints.NOTIFICATIONS);
}

/**
* Updates a notification by calling API.
*/
updateNotification(request: NotificationUpdateRequest, notificationId: string): Observable<Notification> {
const paramsMap: { [key: string]: string } = {
notificationid: notificationId,
};
return this.httpRequestService.put(ResourceEndpoints.NOTIFICATION, paramsMap, request);
}

/**
* Deletes a notification by calling API.
*/
Expand Down

0 comments on commit 7756f04

Please sign in to comment.