yavsc/Yavsc/ApiControllers/BookQueryApiController.cs

194 lines
6 KiB
C#
Raw Normal View History

2016-08-03 23:32:07 +02:00
using System.Collections.Generic;
using System.Linq;
2016-09-05 00:21:48 +02:00
using System.Security.Claims;
2016-08-04 00:06:59 +02:00
using Microsoft.AspNet.Authorization;
2016-08-03 23:32:07 +02:00
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
2016-09-17 03:32:02 +02:00
using Microsoft.Extensions.Logging;
2016-08-03 23:32:07 +02:00
namespace Yavsc.Controllers
{
2016-09-23 12:26:15 +02:00
using System;
using Yavsc.Models.Messaging;
2016-09-17 03:32:02 +02:00
using Yavsc.Models;
2017-02-11 15:46:26 +01:00
using Yavsc.Models.Workflow;
2016-09-17 03:32:02 +02:00
2016-08-03 23:32:07 +02:00
[Produces("application/json")]
2016-09-17 03:32:02 +02:00
[Route("api/bookquery"), Authorize(Roles = "Performer,Administrator")]
2016-08-03 23:32:07 +02:00
public class BookQueryApiController : Controller
{
private ApplicationDbContext _context;
2016-09-17 03:32:02 +02:00
private ILogger _logger;
2016-08-03 23:32:07 +02:00
2016-09-17 03:32:02 +02:00
public BookQueryApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
2016-08-03 23:32:07 +02:00
{
_context = context;
2016-09-17 03:32:02 +02:00
_logger = loggerFactory.CreateLogger<BookQueryApiController>();
2016-08-03 23:32:07 +02:00
}
2016-09-23 12:26:15 +02:00
// 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>
2016-09-17 03:32:02 +02:00
[HttpGet]
2017-02-27 19:28:32 +01:00
public IEnumerable<RdvQueryProviderInfo> GetCommands(long maxId=long.MaxValue)
2016-08-03 23:32:07 +02:00
{
2016-09-05 00:21:48 +02:00
var uid = User.GetUserId();
2016-09-23 12:26:15 +02:00
var now = DateTime.Now;
2017-06-08 00:26:29 +02:00
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).
2017-02-27 19:28:32 +01:00
Select(c => new RdvQueryProviderInfo
2016-09-17 03:32:02 +02:00
{
Client = new ClientProviderInfo {
UserName = c.Client.UserName,
UserId = c.ClientId,
Avatar = c.Client.Avatar },
2016-09-17 03:32:02 +02:00
Location = c.Location,
EventDate = c.EventDate,
Id = c.Id,
Previsional = c.Previsional,
2017-01-16 15:33:11 +01:00
Reason = c.Reason,
ActivityCode = c.ActivityCode,
BillingCode = c.BillingCode
2016-09-23 12:26:15 +02:00
}).
OrderBy(c=>c.Id).
Take(25);
2016-09-17 03:32:02 +02:00
return result;
2016-08-03 23:32:07 +02:00
}
// GET: api/BookQueryApi/5
[HttpGet("{id}", Name = "GetBookQuery")]
public IActionResult GetBookQuery([FromRoute] long id)
{
2016-09-17 03:32:02 +02:00
2016-08-03 23:32:07 +02:00
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
2016-09-05 00:21:48 +02:00
var uid = User.GetUserId();
2016-08-03 23:32:07 +02:00
2017-06-08 00:26:29 +02:00
RdvQuery bookQuery = _context.RdvQueries.Where(c => c.ClientId == uid || c.PerformerId == uid).Single(m => m.Id == id);
2016-08-03 23:32:07 +02:00
if (bookQuery == null)
{
return HttpNotFound();
}
return Ok(bookQuery);
}
// PUT: api/BookQueryApi/5
[HttpPut("{id}")]
2017-02-27 19:28:32 +01:00
public IActionResult PutBookQuery(long id, [FromBody] RdvQuery bookQuery)
2016-08-03 23:32:07 +02:00
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != bookQuery.Id)
{
return HttpBadRequest();
}
2016-09-05 00:21:48 +02:00
var uid = User.GetUserId();
if (bookQuery.ClientId != uid)
2016-09-17 03:32:02 +02:00
return HttpNotFound();
2016-08-03 23:32:07 +02:00
_context.Entry(bookQuery).State = EntityState.Modified;
try
{
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
}
catch (DbUpdateConcurrencyException)
{
if (!BookQueryExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/BookQueryApi
[HttpPost]
2017-02-27 19:28:32 +01:00
public IActionResult PostBookQuery([FromBody] RdvQuery bookQuery)
2016-08-03 23:32:07 +02:00
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
2016-09-05 00:21:48 +02:00
var uid = User.GetUserId();
2016-09-17 03:32:02 +02:00
if (bookQuery.ClientId != uid)
{
ModelState.AddModelError("ClientId", "You must be the client at creating a book query");
2016-09-05 00:21:48 +02:00
return new BadRequestObjectResult(ModelState);
}
2017-06-08 00:26:29 +02:00
_context.RdvQueries.Add(bookQuery);
2016-08-03 23:32:07 +02:00
try
{
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
}
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);
}
2016-09-05 00:21:48 +02:00
var uid = User.GetUserId();
2017-06-08 00:26:29 +02:00
RdvQuery bookQuery = _context.RdvQueries.Single(m => m.Id == id);
2016-09-05 00:21:48 +02:00
2016-08-03 23:32:07 +02:00
if (bookQuery == null)
{
return HttpNotFound();
}
2016-09-05 00:21:48 +02:00
if (bookQuery.ClientId != uid) return HttpNotFound();
2016-08-03 23:32:07 +02:00
2017-06-08 00:26:29 +02:00
_context.RdvQueries.Remove(bookQuery);
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
return Ok(bookQuery);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BookQueryExists(long id)
{
2017-06-08 00:26:29 +02:00
return _context.RdvQueries.Count(e => e.Id == id) > 0;
2016-08-03 23:32:07 +02:00
}
}
}