yavsc/Yavsc/ApiControllers/BookQueryApiController.cs

177 lines
5.3 KiB
C#

8 years ago
using System.Collections.Generic;
using System.Linq;
8 years ago
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
8 years ago
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
8 years ago
using Microsoft.Extensions.Logging;
8 years ago
namespace Yavsc.Controllers
{
8 years ago
using Yavsc.Model;
using Yavsc.Models;
using Yavsc.Models.Booking;
8 years ago
[Produces("application/json")]
8 years ago
[Route("api/bookquery"), Authorize(Roles = "Performer,Administrator")]
8 years ago
public class BookQueryApiController : Controller
{
private ApplicationDbContext _context;
8 years ago
private ILogger _logger;
8 years ago
8 years ago
public BookQueryApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
8 years ago
{
_context = context;
8 years ago
_logger = loggerFactory.CreateLogger<BookQueryApiController>();
8 years ago
}
// GET: api/BookQueryApi
8 years ago
[HttpGet]
public IEnumerable<BookQueryProviderView> GetCommands()
8 years ago
{
8 years ago
var uid = User.GetUserId();
8 years ago
var result = _context.Commands.Include(c => c.Location)
.Include(c => c.Client).Where(c => c.PerformerId == uid).
Select(c => new BookQueryProviderView
{
Client = new ClientProviderView { UserName = c.Client.UserName, UserId = c.ClientId },
Location = c.Location,
EventDate = c.EventDate,
Id = c.Id,
Previsional = c.Previsional
});
return result;
8 years ago
}
// GET: api/BookQueryApi/5
[HttpGet("{id}", Name = "GetBookQuery")]
public IActionResult GetBookQuery([FromRoute] long id)
{
8 years ago
8 years ago
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
8 years ago
var uid = User.GetUserId();
8 years ago
8 years ago
BookQuery bookQuery = _context.Commands.Where(c => c.ClientId == uid || c.PerformerId == uid).Single(m => m.Id == id);
8 years ago
if (bookQuery == null)
{
return HttpNotFound();
}
return Ok(bookQuery);
}
// PUT: api/BookQueryApi/5
[HttpPut("{id}")]
public IActionResult PutBookQuery(long id, [FromBody] BookQuery bookQuery)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != bookQuery.Id)
{
return HttpBadRequest();
}
8 years ago
var uid = User.GetUserId();
if (bookQuery.ClientId != uid)
8 years ago
return HttpNotFound();
8 years ago
_context.Entry(bookQuery).State = EntityState.Modified;
try
{
_context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!BookQueryExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/BookQueryApi
[HttpPost]
public IActionResult PostBookQuery([FromBody] BookQuery bookQuery)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
8 years ago
var uid = User.GetUserId();
8 years ago
if (bookQuery.ClientId != uid)
{
ModelState.AddModelError("ClientId", "You must be the client at creating a book query");
8 years ago
return new BadRequestObjectResult(ModelState);
}
8 years ago
_context.Commands.Add(bookQuery);
try
{
_context.SaveChanges();
}
catch (DbUpdateException)
{
if (BookQueryExists(bookQuery.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtRoute("GetBookQuery", new { id = bookQuery.Id }, bookQuery);
}
// DELETE: api/BookQueryApi/5
[HttpDelete("{id}")]
public IActionResult DeleteBookQuery(long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
8 years ago
var uid = User.GetUserId();
8 years ago
BookQuery bookQuery = _context.Commands.Single(m => m.Id == id);
8 years ago
8 years ago
if (bookQuery == null)
{
return HttpNotFound();
}
8 years ago
if (bookQuery.ClientId != uid) return HttpNotFound();
8 years ago
_context.Commands.Remove(bookQuery);
_context.SaveChanges();
return Ok(bookQuery);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BookQueryExists(long id)
{
return _context.Commands.Count(e => e.Id == id) > 0;
}
}
}