yavsc/src/Yavsc.Api/Controllers/ServiceApiController.cs

147 lines
3.8 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Yavsc.Helpers;
2019-01-01 16:28:47 +00:00
using Yavsc.Models;
using Yavsc.Models.Market;
using Yavsc.Server.Helpers;
2019-01-01 16:28:47 +00:00
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/ServiceApi")]
public class ServiceApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2019-01-01 16:28:47 +00:00
public ServiceApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/ServiceApi
[HttpGet]
public IEnumerable<Service> GetServices()
{
return _context.Services;
}
// GET: api/ServiceApi/5
[HttpGet("{id}", Name = "GetService")]
public IActionResult GetService([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
Service service = _context.Services.Single(m => m.Id == id);
if (service == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
return Ok(service);
}
// PUT: api/ServiceApi/5
2026-05-30 19:34:22 +01:00
[HttpPut("{id}"),Authorize(YavscConstants.FrontOfficeGroupName)]
2019-01-01 16:28:47 +00:00
public IActionResult PutService(long id, [FromBody] Service service)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
if (id != service.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-01-01 16:28:47 +00:00
}
_context.Entry(service).State = EntityState.Modified;
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!ServiceExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2019-01-01 16:28:47 +00:00
}
// POST: api/ServiceApi
2026-05-30 19:34:22 +01:00
[HttpPost,Authorize(YavscConstants.FrontOfficeGroupName)]
2019-01-01 16:28:47 +00:00
public IActionResult PostService([FromBody] Service service)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
_context.Services.Add(service);
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateException)
{
if (ServiceExists(service.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetService", new { id = service.Id }, service);
}
// DELETE: api/ServiceApi/5
2026-05-30 19:34:22 +01:00
[HttpDelete("{id}"),Authorize(YavscConstants.FrontOfficeGroupName)]
2019-01-01 16:28:47 +00:00
public IActionResult DeleteService(long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
Service service = _context.Services.Single(m => m.Id == id);
if (service == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
_context.Services.Remove(service);
_context.SaveChanges(User.GetUserId());
return Ok(service);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool ServiceExists(long id)
{
return _context.Services.Count(e => e.Id == id) > 0;
}
}
2020-10-09 19:35:39 +01:00
}