Commit 845ffe2e authored by Ali's avatar Ali

add notification controller

parent bd5fdf7a
package com.ali.notificationsvc.controllers;
import com.ali.notificationsvc.dtos.CreateNotificationDto;
import com.ali.notificationsvc.dtos.NotificationDto;
import com.ali.notificationsvc.services.INotificationService;
import com.ali.notificationsvc.status.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {
@Autowired
INotificationService notificationService;
public NotificationController(INotificationService iNotificationService) {
this.notificationService = iNotificationService;
}
@GetMapping("getById/{id}")
public ResponseEntity<NotificationDto> getById(@PathVariable("id") int id) {
NotificationDto notificationDto = new NotificationDto();
notificationDto = notificationService.getById(id);
return ResponseEntity.ok(notificationDto);
}
@GetMapping("getAll")
public ResponseEntity<List<NotificationDto>> getAllNotifications() {
List<NotificationDto> notificationDtos = notificationService.getAll();
return ResponseEntity.ok(notificationDtos);
}
@GetMapping("getAllByCompanyId/{id}")
public ResponseEntity<List<NotificationDto>> getAllByCompanyId(@PathVariable("id") int companyId) {
List<NotificationDto> notificationDtos = notificationService.getNotificationByCompanyId(companyId);
return ResponseEntity.ok(notificationDtos);
}
@GetMapping("getAllByJobseekerId/{id}")
public ResponseEntity<List<NotificationDto>> getAllByJobseekerId(@PathVariable("id") int jobseekerId) {
List<NotificationDto> notificationDtos = notificationService.getNotificationByJobseekerId(jobseekerId);
return ResponseEntity.ok(notificationDtos);
}
@GetMapping("isExist/{id}")
public ResponseEntity<Boolean> isExist(@PathVariable("id") int notificationId) {
boolean isExist = notificationService.existById(notificationId);
return ResponseEntity.ok(isExist);
}
@PostMapping("create")
public ResponseEntity<RequestStatus> createNotification(@RequestBody CreateNotificationDto createNotificationDto) {
RequestStatus status = notificationService.create(createNotificationDto);
if (status.isStatus()) {
return ResponseEntity.status(HttpStatus.CREATED).body(status);
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(status);
}
}
@PutMapping("update")
public ResponseEntity<RequestStatus> updateNotification(@RequestBody NotificationDto notificationDto) {
RequestStatus status = notificationService.update(notificationDto);
if (status.isStatus()) {
return ResponseEntity.ok(status);
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(status);
}
}
@DeleteMapping("delete/{id}")
public ResponseEntity<RequestStatus> deleteNotification(@PathVariable("id") int id) {
RequestStatus status = notificationService.delete(id);
if (status.isStatus()) {
return ResponseEntity.ok(status);
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(status);
}
}
}
package com.ali.notificationsvc.dtos;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Builder
public class CreateNotificationDto {
private int jobId;
private int companyId;
......@@ -23,4 +27,5 @@ public class CreateNotificationDto {
public CreateNotificationDto() {
}
}
package com.ali.notificationsvc.dtos;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Builder
public class NotificationDto {
private int id;
private int jobId;
......
package com.ali.notificationsvc.models;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.Date;
@Entity
@Data
@Builder
public class Notification {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int jobId;
private int companyId;
......
......@@ -14,4 +14,5 @@ public interface INotificationRepository extends JpaRepository<Notification, Int
List<Notification> findNotificationsByCompanyId(@Param("companyId") int companyId);
@Query("SELECT i FROM Notification i WHERE i.jobseekerId = :jobseekerId")
List<Notification> findNotificationsByJobseekerId(@Param("jobseekerId") int jobseekerId);
}
......@@ -5,6 +5,7 @@ import com.ali.notificationsvc.dtos.NotificationDto;
import com.ali.notificationsvc.models.Notification;
import com.ali.notificationsvc.repositories.INotificationRepository;
import com.ali.notificationsvc.status.RequestStatus;
import org.modelmapper.Conditions;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -65,8 +66,11 @@ public class NotificationService implements INotificationService {
@Override
public RequestStatus create(CreateNotificationDto cNotificationDto) {
RequestStatus status = new RequestStatus();
ignoreIdMapping(modelMapper);
try {
System.out.println(cNotificationDto.toString());
Notification notification = modelMapper.map(cNotificationDto, Notification.class);
System.out.println(notification.toString());
iNotificationRepository.save(notification);
status.setStatus(true);
status.setDescribtion("created successfully");
......@@ -115,4 +119,16 @@ public class NotificationService implements INotificationService {
public boolean existById(int notificationId) {
return iNotificationRepository.existsById(notificationId);
}
private void ignoreIdMapping(ModelMapper modelMapper){
modelMapper.getConfiguration()
.setPropertyCondition(Conditions.isNotNull())
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE)
.setSkipNullEnabled(true)
.setAmbiguityIgnored(true);
modelMapper.typeMap(CreateNotificationDto.class, Notification.class)
.addMappings(mapper -> {
mapper.skip(Notification::setId);
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment