FormOfWorksController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using NuGet.Protocol.Core.Types;
  9. using OnlineMetodist.API.Data;
  10. using OnlineMetodist.API.Models;
  11. namespace OnlineMetodist.API.Controllers
  12. {
  13. [Route("api/[controller]")]
  14. [ApiController]
  15. public class FormOfWorksController : ControllerBase
  16. {
  17. private readonly OnlineMetodistDbContext _context;
  18. private readonly ILogger<FormOfWorksController> _logger;
  19. public FormOfWorksController(OnlineMetodistDbContext context, ILogger<FormOfWorksController> logger)
  20. {
  21. _context = context;
  22. _logger = logger;
  23. }
  24. // GET: api/FormOfWorks
  25. [HttpGet]
  26. public async Task<ActionResult<IEnumerable<FormOfWork>>> GetFormsOfWorks()
  27. {
  28. try
  29. {
  30. var forms = await _context.FormsOfWorks.ToListAsync();
  31. if (forms == null)
  32. {
  33. _logger.LogError($"FormOfWork object sent from client is null.");
  34. return NotFound("FormOfWork object sent from client is null.");
  35. }
  36. return Ok(forms);
  37. }
  38. catch (Exception ex)
  39. {
  40. _logger.LogError($"Something went wrong inside GetFormsOfWorks action: {ex.Message}");
  41. return StatusCode(500, "Internal server error");
  42. }
  43. }
  44. // GET: api/FormOfWorks/5
  45. [HttpGet("{id}")]
  46. public async Task<ActionResult<FormOfWork>> GetFormOfWork(Guid id)
  47. {
  48. try
  49. {
  50. var formOfWork = await _context.FormsOfWorks.FindAsync(id);
  51. if (formOfWork == null)
  52. {
  53. _logger.LogError($"FormOfWork with id: {id}, hasn't been found in db.");
  54. return NotFound();
  55. }
  56. return Ok(formOfWork);
  57. }
  58. catch (Exception ex)
  59. {
  60. _logger.LogError($"Something went wrong inside GetFormOfWork action: {ex.Message}");
  61. return StatusCode(500, "Internal server error");
  62. }
  63. }
  64. // PUT: api/FormOfWorks/5
  65. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  66. [HttpPut("{id}")]
  67. public async Task<IActionResult> UpdateFormOfWork(Guid id, FormOfWork formOfWork)
  68. {
  69. if (formOfWork is null)
  70. {
  71. _logger.LogError("FormOfWork object sent from client is null.");
  72. return BadRequest("FormOfWork object is null");
  73. }
  74. if (!ModelState.IsValid)
  75. {
  76. _logger.LogError("Invalid FormOfWork object sent from client.");
  77. return BadRequest("Invalid model object");
  78. }
  79. try
  80. {
  81. if (!FormOfWorkExists(id))
  82. {
  83. _logger.LogError("Invalid id sent from client.");
  84. return BadRequest("Invalid id");
  85. }
  86. formOfWork.Id = id;
  87. _context.Entry(formOfWork).State = EntityState.Modified;
  88. await _context.SaveChangesAsync();
  89. return NoContent();
  90. }
  91. catch (Exception ex)
  92. {
  93. _logger.LogError($"Something went wrong inside UpdateFormOfWork action: {ex.Message}");
  94. return StatusCode(500, "Internal server error");
  95. }
  96. }
  97. // // POST: api/FormOfWorks
  98. // // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  99. // [HttpPost]
  100. // public async Task<ActionResult<FormOfWork>> CreateFormOfWork(FormOfWork formOfWork)
  101. // {
  102. //if (formOfWork is null)
  103. //{
  104. // _logger.LogError("FormOfWork object sent from client is null.");
  105. // return BadRequest("FormOfWork object is null");
  106. //}
  107. //if (!ModelState.IsValid)
  108. //{
  109. // _logger.LogError("Invalid FormOfWork object sent from client.");
  110. // return BadRequest("Invalid model object");
  111. //}
  112. //try
  113. //{
  114. // formOfWork.Id = Guid.NewGuid();
  115. // _context.FormsOfWorks.Add(formOfWork);
  116. // _context.SaveChanges();
  117. // return CreatedAtAction("GetFormOfWork", new { id = formOfWork.Id }, formOfWork);
  118. // }
  119. // catch (Exception ex)
  120. // {
  121. // _logger.LogError($"Something went wrong inside CreateFormOfWork action: {ex.Message}");
  122. // return StatusCode(500, "Internal server error");
  123. // }
  124. // }
  125. // DELETE: api/FormOfWorks/5
  126. [HttpDelete("{id}")]
  127. public async Task<IActionResult> DeleteFormOfWork(Guid id)
  128. {
  129. try
  130. {
  131. var formOfWork = await _context.FormsOfWorks.FindAsync(id);
  132. if (formOfWork == null)
  133. {
  134. _logger.LogError($"FormOfWork with id: {id}, hasn't been found in db.");
  135. return NotFound();
  136. }
  137. _context.FormsOfWorks.Remove(formOfWork);
  138. await _context.SaveChangesAsync();
  139. return NoContent();
  140. }
  141. catch (Exception ex)
  142. {
  143. _logger.LogError($"Something went wrong inside DeleteFormOfWork action: {ex.Message}");
  144. return StatusCode(500, "Internal server error");
  145. }
  146. }
  147. private bool FormOfWorkExists(Guid id)
  148. {
  149. return _context.FormsOfWorks.Any(e => e.Id == id);
  150. }
  151. }
  152. }