yavsc/Yavsc/ApiControllers/BookQueryApiController.cs

193 lines
5.9 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
{
using System;
using Yavsc.Models.Messaging;
8 years ago
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
/// <summary>
/// Book queries, by creation order
/// </summary>
/// <param name="maxId">returned Ids must be lower than this value</param>
/// <returns>book queries</returns>
8 years ago
[HttpGet]
8 years ago
public IEnumerable<BookQueryProviderInfo> GetCommands(long maxId=long.MaxValue)
8 years ago
{
8 years ago
var uid = User.GetUserId();
var now = DateTime.Now;
var result = _context.Commands.Include(c => c.Location).
Include(c => c.Client).Where(c => c.PerformerId == uid && c.Id < maxId && c.EventDate > now
&& c.ValidationDate == null).
8 years ago
Select(c => new BookQueryProviderInfo
8 years ago
{
Client = new ClientProviderInfo {
UserName = c.Client.UserName,
UserId = c.ClientId,
Avatar = c.Client.Avatar },
8 years ago
Location = c.Location,
EventDate = c.EventDate,
Id = c.Id,
Previsional = c.Previsional,
8 years ago
Reason = c.Reason,
ActivityCode = c.ActivityCode
}).
OrderBy(c=>c.Id).
Take(25);
8 years ago
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;
}
}
}