-
Notifications
You must be signed in to change notification settings - Fork 0
Add and manage announcements #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,21 @@ | |
package org.gridsuite.useradmin.server; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import org.gridsuite.useradmin.server.dto.UserConnection; | ||
import org.gridsuite.useradmin.server.dto.UserInfos; | ||
import org.gridsuite.useradmin.server.repository.AnnouncementEntity; | ||
import org.gridsuite.useradmin.server.service.UserAdminService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author Etienne Homer <etienne.homer at rte-france.com> | ||
|
@@ -109,27 +110,38 @@ public ResponseEntity<List<UserConnection>> getConnections(@RequestHeader("userI | |
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(service.getConnections(userId)); | ||
} | ||
|
||
@PostMapping(value = "/messages/maintenance") | ||
@Operation(summary = "send a message to all users connected") | ||
@PostMapping(value = "/announcements") | ||
AbdelHedhili marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Operation(summary = "Send a message to all the connected users") | ||
AbdelHedhili marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "message sent"), | ||
@ApiResponse(responseCode = "403", description = "user is not an admin") | ||
}) | ||
public ResponseEntity<Void> sendMaintenanceMessage(@RequestHeader("userId") String userId, | ||
AbdelHedhili marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@Parameter(description = "the display time of the message in seconds") @RequestParam(value = "durationInSeconds", required = false) Integer duration, | ||
@Parameter(description = "the message to display") @RequestBody String message) { | ||
service.sendMaintenanceMessage(userId, duration, message); | ||
@RequestBody AnnouncementEntity announcement) { | ||
service.sendAnnouncement(announcement, userId); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping(value = "/messages/cancel-maintenance") | ||
@Operation(summary = "send a message to all users connected") | ||
@DeleteMapping(value = "/announcements/{announcementId}") | ||
@Operation(summary = "Cancel and delete a message") | ||
|
||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "message sent"), | ||
@ApiResponse(responseCode = "403", description = "user is not an admin") | ||
@ApiResponse(responseCode = "200", description = "message canceled"), | ||
@ApiResponse(responseCode = "403", description = "user is not an admin"), | ||
@ApiResponse(responseCode = "404", description = "message not found") | ||
}) | ||
public ResponseEntity<Void> sendCancelMaintenanceMessage(@RequestHeader("userId") String userId) { | ||
service.sendCancelMaintenanceMessage(userId); | ||
public ResponseEntity<Void> sendCancelMaintenanceMessage(@RequestHeader("userId") String userId, | ||
@PathVariable("announcementId") UUID announcementId) { | ||
service.cancelAnnouncement(announcementId, userId); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping(value = "/announcements") | ||
@Operation(summary = "Get all the messages") | ||
AbdelHedhili marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "messages retrieved"), | ||
@ApiResponse(responseCode = "403", description = "user is not an admin") | ||
}) | ||
public ResponseEntity<List<AnnouncementEntity>> sendCancelMaintenanceMessage(@RequestHeader("userId") String userId) { | ||
AbdelHedhili marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return ResponseEntity.ok(service.getAllAnnouncements(userId)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.gridsuite.useradmin.server.repository; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author Florent MILLOT <florent.millot at rte-france.com> | ||
*/ | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "announcement") | ||
public class AnnouncementEntity { | ||
|
||
public AnnouncementEntity(String message, Duration duration) { | ||
this.message = message; | ||
this.duration = duration; | ||
} | ||
|
||
@Id | ||
@GeneratedValue | ||
@Column | ||
private UUID id; | ||
|
||
@Column(nullable = false) | ||
private Instant creationDate = Instant.now(); | ||
|
||
@Column(nullable = false) | ||
private String message; | ||
|
||
@Column | ||
private Duration duration; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.gridsuite.useradmin.server.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @author Florent MILLOT <florent.millot at rte-france.com> | ||
*/ | ||
@Repository | ||
public interface AnnouncementRepository extends JpaRepository<AnnouncementEntity, UUID> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.gridsuite.useradmin.server.service; | ||
|
||
import org.gridsuite.useradmin.server.repository.AnnouncementEntity; | ||
import org.gridsuite.useradmin.server.repository.AnnouncementRepository; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author Florent MILLOT <florent.millot at rte-france.com> | ||
*/ | ||
@Service | ||
public class AnnouncementService { | ||
|
||
private final AnnouncementRepository announcementRepository; | ||
private final NotificationService notificationService; | ||
|
||
public AnnouncementService(final AnnouncementRepository announcementRepository, | ||
final NotificationService notificationService) { | ||
this.announcementRepository = Objects.requireNonNull(announcementRepository); | ||
this.notificationService = Objects.requireNonNull(notificationService); | ||
} | ||
|
||
@Transactional | ||
public void sendAnnouncement(AnnouncementEntity announcement) { | ||
this.announcementRepository.deleteAll(); // for now, only one message at a time | ||
this.announcementRepository.save(announcement); | ||
if (announcement.getDuration() == null) { | ||
notificationService.emitMaintenanceMessage(announcement.getMessage()); | ||
} else { | ||
notificationService.emitMaintenanceMessage(announcement.getMessage(), announcement.getDuration().toSeconds()); | ||
} | ||
Comment on lines
+40
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this can be handled in the EmitMessage method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have done that also, but it's not a big deal and does not change that much so I kept the existing code |
||
} | ||
|
||
@Transactional | ||
public void cancelAnnouncement(UUID announcementId) { | ||
if (!announcementRepository.existsById(announcementId)) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find announcement"); | ||
} | ||
this.announcementRepository.deleteById(announcementId); | ||
notificationService.emitCancelMaintenanceMessage(); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<AnnouncementEntity> getAllAnnouncements() { | ||
return this.announcementRepository.findAll(); | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you create AnnouncementController then you can connect AnnoucementController and AnnouncementService directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, cf above |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.1" encoding="UTF-8" standalone="no"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> | ||
<changeSet author="florent (generated)" id="1711719892005-1"> | ||
<createTable tableName="announcement"> | ||
<column name="id" type="UUID"> | ||
<constraints nullable="false" primaryKey="true" primaryKeyName="announcementPK"/> | ||
</column> | ||
<column name="creation_date" type="TIMESTAMP(6)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
<column name="duration" type="numeric(21, 0)"/> | ||
<column name="message" type="VARCHAR(255)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
</databaseChangeLog> |
Uh oh!
There was an error while loading. Please reload this page.