-
Notifications
You must be signed in to change notification settings - Fork 0
added Notifications Controller #8
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
Open
NanRazvan
wants to merge
1
commit into
master
Choose a base branch
from
notifications-controller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/demo1/controller/NotificationsController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.example.demo1.controller; | ||
|
|
||
|
|
||
| import com.example.demo1.model.Notification; | ||
| import com.example.demo1.model.notificationsType.SmsNotification; | ||
| import com.example.demo1.service.impl.NotificationService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| // | ||
| @Controller | ||
| @RequestMapping("/notifications") | ||
| public class NotificationsController { | ||
|
|
||
|
|
||
| private final NotificationService notificationService; | ||
|
|
||
| @Autowired | ||
| public NotificationsController(NotificationService notificationService) { | ||
| this.notificationService = notificationService; | ||
| } | ||
|
|
||
| @GetMapping() | ||
| public String getAllNotifications(Model model){ | ||
| model.addAttribute("notifications", notificationService.getAllNotifications()); | ||
| return "notificationsView/notifications"; | ||
|
|
||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public String getNotificationById(@PathVariable("id") int id,Model model){ | ||
| model.addAttribute("notifications", notificationService.getNotificationById(id)); | ||
| return "notificationsView/notifications"; | ||
| } | ||
|
|
||
|
|
||
| @PostMapping("/add") | ||
| @ResponseBody | ||
| public String addNotification(){ | ||
| SmsNotification notification = notificationService.addNotification(); | ||
|
|
||
| return notification.toString() + "- notification added"; | ||
| } | ||
|
|
||
| @PutMapping("/update/{id}") | ||
| @ResponseBody | ||
| public String updateById(@PathVariable("id") int id){ | ||
|
|
||
| //notificationService.updateById(); | ||
|
|
||
| return "Notification with id "+ id +" has been updated"; | ||
| } | ||
|
|
||
|
|
||
| @DeleteMapping("/delete") | ||
| @ResponseBody | ||
| public String deleteAll(){ | ||
| //notificationService.deleteAll(); | ||
| return "Notifications have been deleted"; | ||
| } | ||
|
|
||
| @DeleteMapping("/delete/{id}") | ||
| @ResponseBody | ||
| public String deleteById(@PathVariable("id") int id){ | ||
| //notificationService.deleteById(); | ||
| return "Notification with id " + id + " have been deleted"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.example.demo1.model; | ||
|
|
||
| import lombok.Data; | ||
| import lombok.experimental.SuperBuilder; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| @Data | ||
| @SuperBuilder | ||
| public abstract class Notification { | ||
|
|
||
| protected int id; | ||
| protected String message; | ||
| protected Date timestamp; | ||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/demo1/model/notificationsType/EmailNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.demo1.model.notificationsType; | ||
|
|
||
| import com.example.demo1.model.Notification; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.ToString; | ||
| import lombok.experimental.SuperBuilder; | ||
|
|
||
| @Data | ||
| @SuperBuilder | ||
| @ToString(callSuper = true) | ||
| public class EmailNotification extends Notification { | ||
|
|
||
| private String recipientEmail; | ||
| private String senderEmail; | ||
| private String subject; | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/demo1/model/notificationsType/SmsNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.example.demo1.model.notificationsType; | ||
|
|
||
| import com.example.demo1.model.Notification; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.ToString; | ||
| import lombok.experimental.SuperBuilder; | ||
|
|
||
| @Data | ||
| @SuperBuilder | ||
| @ToString(callSuper = true) | ||
| public class SmsNotification extends Notification { | ||
|
|
||
| private String recipientNumber; | ||
| private String senderNumber; | ||
|
|
||
| } |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/example/demo1/service/impl/NotificationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.example.demo1.service.impl; | ||
|
|
||
| import com.example.demo1.model.Notification; | ||
| import com.example.demo1.model.notificationsType.EmailNotification; | ||
| import com.example.demo1.model.notificationsType.SmsNotification; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class NotificationService { | ||
|
|
||
|
|
||
| List<Notification> notificationList = new ArrayList<>(); | ||
|
|
||
| public List<Notification> getAllNotifications() { | ||
|
|
||
| List<Notification> notificationsList = new ArrayList<>(); | ||
|
|
||
| EmailNotification emailNotification1 = EmailNotification.builder() | ||
| .id(1) | ||
| .message("Email marketing message1") | ||
| .recipientEmail("[email protected]") | ||
| .senderEmail("[email protected]") | ||
| .timestamp(new Date()) | ||
| .build(); | ||
|
|
||
| EmailNotification emailNotification2 = EmailNotification.builder() | ||
| .id(2) | ||
| .message("Email marketing message2") | ||
| .recipientEmail("[email protected]") | ||
| .senderEmail("[email protected]") | ||
| .timestamp(new Date()) | ||
| .build(); | ||
|
|
||
| SmsNotification smsNotification1 = SmsNotification.builder() | ||
| .id(3) | ||
| .message("SMS marketing message1") | ||
| .recipientNumber("07phoneNumber") | ||
| .senderNumber("07myPhoneNumber") | ||
| .timestamp(new Date()) | ||
| .build(); | ||
|
|
||
| notificationsList.add(emailNotification1); | ||
| notificationsList.add(emailNotification2); | ||
| notificationsList.add(smsNotification1); | ||
| return notificationsList; | ||
|
|
||
| } | ||
|
|
||
| public List<Notification> getNotificationById(int id){ | ||
| //TODO: Get the notification from the database by the id | ||
|
|
||
| //functie facuta doar sa afisez ceva | ||
| List<Notification> notificationsList = new ArrayList<>(); | ||
|
|
||
| notificationsList.add( SmsNotification.builder() | ||
| .id(id) | ||
| .message("SMS marketing message1") | ||
| .recipientNumber("07phoneNumber") | ||
| .senderNumber("07myPhoneNumber") | ||
| .timestamp(new Date()) | ||
| .build()); | ||
|
|
||
| return notificationsList; | ||
| } | ||
|
|
||
| public SmsNotification addNotification() { | ||
|
|
||
| //TODO: Add notification to the database | ||
|
|
||
| return SmsNotification.builder() | ||
| .id(4) | ||
| .message("SMS marketing message2") | ||
| .recipientNumber("07123phoneNumber") | ||
| .senderNumber("07123myPhoneNumber") | ||
| .timestamp(new Date()) | ||
| .build(); | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
src/main/resources/templates/notificationsView/notifications.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
|
|
||
| <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> | ||
| <title>Notifications</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <div class="container mt-5"> | ||
| <h1 class="mb-4">Notifications List</h1> | ||
|
|
||
| <table class="table table-bordered table-hover"> | ||
| <thead class="thead-dark"> | ||
| <tr> | ||
| <th>ID</th> | ||
| <th>Type</th> | ||
| <th>Message</th> | ||
| <th>Recipient</th> | ||
| <th>Sender</th> | ||
| <th>Timestamp</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody th:each="notification : ${notifications}"> | ||
| <tr> | ||
| <td th:text="${notification.id}"></td> | ||
| <td th:text="${notification instanceof T(com.example.demo1.model.notificationsType.EmailNotification) ? 'Email' : 'SMS'}"></td> | ||
| <td th:text="${notification.message}"></td> | ||
| <td th:text="${notification instanceof T(com.example.demo1.model.notificationsType.EmailNotification) ? notification.recipientEmail : notification.recipientNumber}"></td> | ||
| <td th:text="${notification instanceof T(com.example.demo1.model.notificationsType.EmailNotification) ? notification.senderEmail : notification.senderNumber}"></td> | ||
| <td th:text="${#dates.format(notification.timestamp, 'yyyy-MM-dd HH:mm:ss')}"></td> | ||
| </tr> | ||
|
Owner
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. missing the other views? |
||
| </tbody> | ||
| </table> | ||
| </div> | ||
|
|
||
|
|
||
| </body> | ||
| </html> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Data objects from repositories, keep business logic only here