Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *** CRUD request model for notification ***
- public partial class NotificationRequest
- {
- public string Receiver { get; set; }
- public string Subject { get; set; }
- public string Body { get; set; }
- }
- // *** CRUD response model for notification ***
- public partial class NotificationResponse
- {
- public int Id { get; set; }
- public Guid Guid { get; set; }
- [Required]
- public string Receiver { get; set; }
- [Required]
- public string Subject { get; set; }
- [Required]
- public string Body { get; set; }
- }
- // *** CRUD GetAll() method implementation ***
- [HttpGet("[action]")]
- public ActionResult<IEnumerable<NotificationResponse>> GetAll()
- {
- try
- {
- var allNotifications =
- _dbContext.Notifications.Select(dbNotification =>
- new NotificationResponse
- {
- Id = dbNotification.Id,
- Guid = dbNotification.Guid,
- Receiver = dbNotification.Receiver,
- Subject = dbNotification.Subject,
- Body = dbNotification.Body
- });
- return Ok(allNotifications);
- }
- catch (Exception)
- {
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- // *** CRUD HTTP GET method implementation ***
- [HttpGet("{id}")]
- public ActionResult<NotificationResponse> Get(int id)
- {
- try
- {
- var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
- if(dbNotification == null)
- return NotFound();
- return Ok(new NotificationResponse {
- Id = dbNotification.Id,
- Guid = dbNotification.Guid,
- Receiver = dbNotification.Receiver,
- Subject = dbNotification.Subject,
- Body = dbNotification.Body
- });
- }
- catch (Exception)
- {
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- // *** CRUD HTTP POST method implementation ***
- [HttpPost()]
- public ActionResult<NotificationResponse> Create(NotificationRequest request)
- {
- try
- {
- if (!ModelState.IsValid)
- return BadRequest(ModelState);
- var dbNotification = new Notification {
- Receiver = request.Receiver,
- Subject = request.Subject,
- Body = request.Body
- };
- _dbContext.Notifications.Add(dbNotification);
- _dbContext.SaveChanges();
- return Ok(new NotificationResponse {
- Id = dbNotification.Id,
- Guid = dbNotification.Guid,
- Receiver = dbNotification.Receiver,
- Subject = dbNotification.Subject,
- Body = dbNotification.Body
- });
- }
- catch (Exception)
- {
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- // *** CRUD HTTP PUT method implementation ***
- [HttpPut("{id}")]
- public ActionResult<NotificationResponse> Modify(int id, [FromBody]NotificationRequest request)
- {
- try
- {
- if(!ModelState.IsValid)
- return BadRequest(ModelState);
- var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
- if (dbNotification == null)
- return NotFound();
- dbNotification.Receiver = request.Receiver;
- dbNotification.Subject = request.Subject;
- dbNotification.Body = request.Body;
- dbNotification.UpdatedAt = DateTime.UtcNow;
- _dbContext.SaveChanges();
- return Ok(new NotificationResponse
- {
- Id = dbNotification.Id,
- Guid = dbNotification.Guid,
- Receiver = dbNotification.Receiver,
- Subject = dbNotification.Subject,
- Body = dbNotification.Body
- });
- }
- catch (Exception)
- {
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- // *** CRUD HTTP DELETE method implementation ***
- [HttpDelete("{id}")]
- public ActionResult<NotificationResponse> Remove(int id)
- {
- try
- {
- var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
- if (dbNotification == null)
- return NotFound();
- _dbContext.Notifications.Remove(dbNotification);
- _dbContext.SaveChanges();
- return Ok(new NotificationResponse
- {
- Id = dbNotification.Id,
- Guid = dbNotification.Guid,
- Receiver = dbNotification.Receiver,
- Subject = dbNotification.Subject,
- Body = dbNotification.Body
- });
- }
- catch (Exception)
- {
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement