Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}



}
16 changes: 16 additions & 0 deletions src/main/java/com/example/demo1/model/Notification.java
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;

}
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;

}
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;

}
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()
Copy link
Owner

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

.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 src/main/resources/templates/notificationsView/notifications.html
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>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the other views?

</tbody>
</table>
</div>


</body>
</html>