123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using NuGet.Protocol.Core.Types;
- using OnlineMetodist.API.Data;
- using OnlineMetodist.API.Models;
- namespace OnlineMetodist.API.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class FormOfWorksController : ControllerBase
- {
- private readonly OnlineMetodistDbContext _context;
- private readonly ILogger<FormOfWorksController> _logger;
- public FormOfWorksController(OnlineMetodistDbContext context, ILogger<FormOfWorksController> logger)
- {
- _context = context;
- _logger = logger;
- }
- // GET: api/FormOfWorks
- [HttpGet]
- public async Task<ActionResult<IEnumerable<FormOfWork>>> GetFormsOfWorks()
- {
- try
- {
- var forms = await _context.FormsOfWorks.ToListAsync();
- if (forms == null)
- {
- _logger.LogError($"FormOfWork object sent from client is null.");
- return NotFound("FormOfWork object sent from client is null.");
- }
- return Ok(forms);
- }
- catch (Exception ex)
- {
- _logger.LogError($"Something went wrong inside GetFormsOfWorks action: {ex.Message}");
- return StatusCode(500, "Internal server error");
- }
- }
- // GET: api/FormOfWorks/5
- [HttpGet("{id}")]
- public async Task<ActionResult<FormOfWork>> GetFormOfWork(Guid id)
- {
- try
- {
- var formOfWork = await _context.FormsOfWorks.FindAsync(id);
- if (formOfWork == null)
- {
- _logger.LogError($"FormOfWork with id: {id}, hasn't been found in db.");
- return NotFound();
- }
- return Ok(formOfWork);
- }
- catch (Exception ex)
- {
- _logger.LogError($"Something went wrong inside GetFormOfWork action: {ex.Message}");
- return StatusCode(500, "Internal server error");
- }
- }
- // PUT: api/FormOfWorks/5
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPut("{id}")]
- public async Task<IActionResult> UpdateFormOfWork(Guid id, FormOfWork formOfWork)
- {
-
- if (formOfWork is null)
- {
- _logger.LogError("FormOfWork object sent from client is null.");
- return BadRequest("FormOfWork object is null");
- }
- if (!ModelState.IsValid)
- {
- _logger.LogError("Invalid FormOfWork object sent from client.");
- return BadRequest("Invalid model object");
- }
- try
- {
- if (!FormOfWorkExists(id))
- {
- _logger.LogError("Invalid id sent from client.");
- return BadRequest("Invalid id");
- }
- formOfWork.Id = id;
- _context.Entry(formOfWork).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- return NoContent();
- }
- catch (Exception ex)
- {
- _logger.LogError($"Something went wrong inside UpdateFormOfWork action: {ex.Message}");
- return StatusCode(500, "Internal server error");
- }
- }
- // // POST: api/FormOfWorks
- // // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- // [HttpPost]
- // public async Task<ActionResult<FormOfWork>> CreateFormOfWork(FormOfWork formOfWork)
- // {
- //if (formOfWork is null)
- //{
- // _logger.LogError("FormOfWork object sent from client is null.");
- // return BadRequest("FormOfWork object is null");
- //}
- //if (!ModelState.IsValid)
- //{
- // _logger.LogError("Invalid FormOfWork object sent from client.");
- // return BadRequest("Invalid model object");
- //}
- //try
- //{
- // formOfWork.Id = Guid.NewGuid();
- // _context.FormsOfWorks.Add(formOfWork);
- // _context.SaveChanges();
- // return CreatedAtAction("GetFormOfWork", new { id = formOfWork.Id }, formOfWork);
- // }
- // catch (Exception ex)
- // {
- // _logger.LogError($"Something went wrong inside CreateFormOfWork action: {ex.Message}");
- // return StatusCode(500, "Internal server error");
- // }
- // }
- // DELETE: api/FormOfWorks/5
- [HttpDelete("{id}")]
- public async Task<IActionResult> DeleteFormOfWork(Guid id)
- {
- try
- {
- var formOfWork = await _context.FormsOfWorks.FindAsync(id);
- if (formOfWork == null)
- {
- _logger.LogError($"FormOfWork with id: {id}, hasn't been found in db.");
- return NotFound();
- }
- _context.FormsOfWorks.Remove(formOfWork);
- await _context.SaveChangesAsync();
- return NoContent();
- }
- catch (Exception ex)
- {
- _logger.LogError($"Something went wrong inside DeleteFormOfWork action: {ex.Message}");
- return StatusCode(500, "Internal server error");
- }
- }
- private bool FormOfWorkExists(Guid id)
- {
- return _context.FormsOfWorks.Any(e => e.Id == id);
- }
- }
- }
|