yavsc/Yavsc/ApiControllers/BookQueryApiController.cs

195 lines
6.0 KiB
C#

9 years ago
using System.Collections.Generic;
using System.Linq;
9 years ago
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
9 years ago
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
9 years ago
using Microsoft.Extensions.Logging;
9 years ago
namespace Yavsc.Controllers
{
using System;
9 years ago
using Yavsc.Models;
9 years ago
using Yavsc.Models.Workflow;
8 years ago
using Yavsc.Models.Billing;
using Yavsc.Abstract.Identity;
9 years ago
9 years ago
[Produces("application/json")]
9 years ago
[Route("api/bookquery"), Authorize(Roles = "Performer,Administrator")]
9 years ago
public class BookQueryApiController : Controller
{
private ApplicationDbContext _context;
9 years ago
private ILogger _logger;
9 years ago
9 years ago
public BookQueryApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
9 years ago
{
_context = context;
9 years ago
_logger = loggerFactory.CreateLogger<BookQueryApiController>();
9 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>
9 years ago
[HttpGet]
9 years ago
public IEnumerable<RdvQueryProviderInfo> GetCommands(long maxId=long.MaxValue)
9 years ago
{
9 years ago
var uid = User.GetUserId();
var now = DateTime.Now;
9 years ago
var result = _context.RdvQueries.Include(c => c.Location).
Include(c => c.Client).Where(c => c.PerformerId == uid && c.Id < maxId && c.EventDate > now
&& c.ValidationDate == null).
9 years ago
Select(c => new RdvQueryProviderInfo
9 years ago
{
Client = new ClientProviderInfo {
UserName = c.Client.UserName,
UserId = c.ClientId,
Avatar = c.Client.Avatar },
9 years ago
Location = c.Location,
EventDate = c.EventDate,
Id = c.Id,
Previsional = c.Previsional,
9 years ago
Reason = c.Reason,
ActivityCode = c.ActivityCode,
8 years ago
BillingCode = BillingCodes.Rdv
}).
OrderBy(c=>c.Id).
Take(25);
9 years ago
return result;
9 years ago
}
// GET: api/BookQueryApi/5
[HttpGet("{id}", Name = "GetBookQuery")]
public IActionResult GetBookQuery([FromRoute] long id)
{
9 years ago
9 years ago
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
9 years ago
var uid = User.GetUserId();
9 years ago
9 years ago
RdvQuery bookQuery = _context.RdvQueries.Where(c => c.ClientId == uid || c.PerformerId == uid).Single(m => m.Id == id);
9 years ago
if (bookQuery == null)
{
return HttpNotFound();
}
return Ok(bookQuery);
}
// PUT: api/BookQueryApi/5
[HttpPut("{id}")]
9 years ago
public IActionResult PutBookQuery(long id, [FromBody] RdvQuery bookQuery)
9 years ago
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != bookQuery.Id)
{
return HttpBadRequest();
}
9 years ago
var uid = User.GetUserId();
if (bookQuery.ClientId != uid)
9 years ago
return HttpNotFound();
9 years ago
_context.Entry(bookQuery).State = EntityState.Modified;
try
{
_context.SaveChanges(User.GetUserId());
9 years ago
}
catch (DbUpdateConcurrencyException)
{
if (!BookQueryExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/BookQueryApi
[HttpPost]
9 years ago
public IActionResult PostBookQuery([FromBody] RdvQuery bookQuery)
9 years ago
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
9 years ago
var uid = User.GetUserId();
9 years ago
if (bookQuery.ClientId != uid)
{
ModelState.AddModelError("ClientId", "You must be the client at creating a book query");
9 years ago
return new BadRequestObjectResult(ModelState);
}
9 years ago
_context.RdvQueries.Add(bookQuery);
9 years ago
try
{
_context.SaveChanges(User.GetUserId());
9 years ago
}
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);
}
9 years ago
var uid = User.GetUserId();
9 years ago
RdvQuery bookQuery = _context.RdvQueries.Single(m => m.Id == id);
9 years ago
9 years ago
if (bookQuery == null)
{
return HttpNotFound();
}
9 years ago
if (bookQuery.ClientId != uid) return HttpNotFound();
9 years ago
9 years ago
_context.RdvQueries.Remove(bookQuery);
_context.SaveChanges(User.GetUserId());
9 years ago
return Ok(bookQuery);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BookQueryExists(long id)
{
9 years ago
return _context.RdvQueries.Count(e => e.Id == id) > 0;
9 years ago
}
}
}