Advertisement
otkalce

Entity Framework - simple CRUD

Mar 28th, 2023 (edited)
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | Source Code | 0 0
  1. // *** CRUD request model for notification ***
  2. public partial class NotificationRequest
  3. {
  4.     public string Receiver { get; set; }
  5.  
  6.     public string Subject { get; set; }
  7.  
  8.     public string Body { get; set; }
  9. }
  10.  
  11. // *** CRUD response model for notification ***
  12. public partial class NotificationResponse
  13. {
  14.     public int Id { get; set; }
  15.  
  16.     public Guid Guid { get; set; }
  17.  
  18.     [Required]
  19.     public string Receiver { get; set; }
  20.  
  21.     [Required]
  22.     public string Subject { get; set; }
  23.  
  24.     [Required]
  25.     public string Body { get; set; }
  26.  
  27. }
  28.  
  29. // *** CRUD GetAll() method implementation ***
  30. [HttpGet("[action]")]
  31. public ActionResult<IEnumerable<NotificationResponse>> GetAll()
  32. {
  33.     try
  34.     {
  35.         var allNotifications =
  36.             _dbContext.Notifications.Select(dbNotification =>
  37.             new NotificationResponse
  38.             {
  39.                 Id = dbNotification.Id,
  40.                 Guid = dbNotification.Guid,
  41.                 Receiver = dbNotification.Receiver,
  42.                 Subject = dbNotification.Subject,
  43.                 Body = dbNotification.Body
  44.             });
  45.         return Ok(allNotifications);
  46.     }
  47.     catch (Exception)
  48.     {
  49.         return StatusCode(StatusCodes.Status500InternalServerError);
  50.     }
  51. }
  52.  
  53. // *** CRUD HTTP GET method implementation ***
  54. [HttpGet("{id}")]
  55. public ActionResult<NotificationResponse> Get(int id)
  56. {
  57.     try
  58.     {
  59.         var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
  60.         if(dbNotification == null)
  61.             return NotFound();
  62.                
  63.         return Ok(new NotificationResponse {
  64.             Id = dbNotification.Id,
  65.             Guid = dbNotification.Guid,
  66.             Receiver = dbNotification.Receiver,
  67.             Subject = dbNotification.Subject,
  68.             Body = dbNotification.Body
  69.         });
  70.     }
  71.     catch (Exception)
  72.     {
  73.         return StatusCode(StatusCodes.Status500InternalServerError);
  74.     }
  75. }
  76.  
  77. // *** CRUD HTTP POST method implementation ***
  78. [HttpPost()]
  79. public ActionResult<NotificationResponse> Create(NotificationRequest request)
  80. {
  81.     try
  82.     {
  83.         if (!ModelState.IsValid)
  84.             return BadRequest(ModelState);
  85.  
  86.         var dbNotification = new Notification {
  87.             Receiver = request.Receiver,
  88.             Subject = request.Subject,
  89.             Body = request.Body
  90.         };
  91.  
  92.         _dbContext.Notifications.Add(dbNotification);
  93.  
  94.         _dbContext.SaveChanges();
  95.  
  96.         return Ok(new NotificationResponse {
  97.             Id = dbNotification.Id,
  98.             Guid = dbNotification.Guid,
  99.             Receiver = dbNotification.Receiver,
  100.             Subject = dbNotification.Subject,
  101.             Body = dbNotification.Body
  102.         });
  103.     }
  104.     catch (Exception)
  105.     {
  106.         return StatusCode(StatusCodes.Status500InternalServerError);
  107.     }
  108. }
  109.  
  110. // *** CRUD HTTP PUT method implementation ***
  111. [HttpPut("{id}")]
  112. public ActionResult<NotificationResponse> Modify(int id, [FromBody]NotificationRequest request)
  113. {
  114.     try
  115.     {
  116.         if(!ModelState.IsValid)
  117.             return BadRequest(ModelState);
  118.  
  119.         var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
  120.         if (dbNotification == null)
  121.             return NotFound();
  122.  
  123.         dbNotification.Receiver = request.Receiver;
  124.         dbNotification.Subject = request.Subject;
  125.         dbNotification.Body = request.Body;
  126.         dbNotification.UpdatedAt = DateTime.UtcNow;
  127.  
  128.         _dbContext.SaveChanges();
  129.  
  130.         return Ok(new NotificationResponse
  131.         {
  132.             Id = dbNotification.Id,
  133.             Guid = dbNotification.Guid,
  134.             Receiver = dbNotification.Receiver,
  135.             Subject = dbNotification.Subject,
  136.             Body = dbNotification.Body
  137.         });
  138.     }
  139.     catch (Exception)
  140.     {
  141.         return StatusCode(StatusCodes.Status500InternalServerError);
  142.     }
  143. }
  144.  
  145. // *** CRUD HTTP DELETE method implementation ***
  146. [HttpDelete("{id}")]
  147. public ActionResult<NotificationResponse> Remove(int id)
  148. {
  149.     try
  150.     {
  151.         var dbNotification = _dbContext.Notifications.FirstOrDefault(x => x.Id == id);
  152.         if (dbNotification == null)
  153.             return NotFound();
  154.  
  155.         _dbContext.Notifications.Remove(dbNotification);
  156.  
  157.         _dbContext.SaveChanges();
  158.  
  159.         return Ok(new NotificationResponse
  160.         {
  161.             Id = dbNotification.Id,
  162.             Guid = dbNotification.Guid,
  163.             Receiver = dbNotification.Receiver,
  164.             Subject = dbNotification.Subject,
  165.             Body = dbNotification.Body
  166.         });
  167.     }
  168.     catch (Exception)
  169.     {
  170.         return StatusCode(StatusCodes.Status500InternalServerError);
  171.     }
  172. }
  173.  
Tags: ef-usage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement