From ef5f8148cbdc763f1eec31f8539c4f5ec90344b6 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 1 Sep 2016 18:40:20 +0200 Subject: [PATCH] controller to get and admin services --- Yavsc/ApiControllers/ServiceApiController.cs | 148 +++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 Yavsc/ApiControllers/ServiceApiController.cs diff --git a/Yavsc/ApiControllers/ServiceApiController.cs b/Yavsc/ApiControllers/ServiceApiController.cs new file mode 100644 index 00000000..bf1b2417 --- /dev/null +++ b/Yavsc/ApiControllers/ServiceApiController.cs @@ -0,0 +1,148 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Authorization; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Models; +using Yavsc.Models.Market; + +namespace Yavsc.Controllers +{ + [Produces("application/json")] + [Route("api/ServiceApi")] + public class ServiceApiController : Controller + { + private ApplicationDbContext _context; + + public ServiceApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/ServiceApi + [HttpGet] + public IEnumerable GetServices() + { + return _context.Services; + } + + // GET: api/ServiceApi/5 + [HttpGet("{id}", Name = "GetService")] + public IActionResult GetService([FromRoute] long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + Service service = _context.Services.Single(m => m.Id == id); + + if (service == null) + { + return HttpNotFound(); + } + + return Ok(service); + } + + // PUT: api/ServiceApi/5 + [HttpPut("{id}"),Authorize(Constants.FrontOfficeGroupName)] + public IActionResult PutService(long id, [FromBody] Service service) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (id != service.Id) + { + return HttpBadRequest(); + } + + _context.Entry(service).State = EntityState.Modified; + + try + { + _context.SaveChanges(); + } + catch (DbUpdateConcurrencyException) + { + if (!ServiceExists(id)) + { + return HttpNotFound(); + } + else + { + throw; + } + } + + return new HttpStatusCodeResult(StatusCodes.Status204NoContent); + } + + // POST: api/ServiceApi + [HttpPost,Authorize(Constants.FrontOfficeGroupName)] + public IActionResult PostService([FromBody] Service service) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + _context.Services.Add(service); + try + { + _context.SaveChanges(); + } + catch (DbUpdateException) + { + if (ServiceExists(service.Id)) + { + return new HttpStatusCodeResult(StatusCodes.Status409Conflict); + } + else + { + throw; + } + } + + return CreatedAtRoute("GetService", new { id = service.Id }, service); + } + + // DELETE: api/ServiceApi/5 + [HttpDelete("{id}"),Authorize(Constants.FrontOfficeGroupName)] + public IActionResult DeleteService(long id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + Service service = _context.Services.Single(m => m.Id == id); + if (service == null) + { + return HttpNotFound(); + } + + _context.Services.Remove(service); + _context.SaveChanges(); + + 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; + } + } +} \ No newline at end of file