yavsc/src/Yavsc.Api/Controllers/HairCut/HairCutController.cs

229 lines
7.2 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
2017-03-15 07:09:48 +01:00
using Microsoft.Extensions.Localization;
2017-05-17 01:41:52 +02:00
2017-03-15 07:09:48 +01:00
namespace Yavsc.ApiControllers
{
2017-05-27 16:22:58 +02:00
using Yavsc;
2017-03-15 07:09:48 +01:00
using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.Extensions.Logging;
using Models;
using Services;
2017-05-15 13:36:14 +02:00
using Models.Haircut;
2017-05-12 12:15:57 +02:00
using System.Threading.Tasks;
2017-05-15 13:36:14 +02:00
using Helpers;
using Models.Payment;
2017-05-17 01:41:52 +02:00
using Newtonsoft.Json;
2017-05-24 23:36:49 +02:00
using PayPal.PayPalAPIInterfaceService.Model;
using Yavsc.Models.Haircut.Views;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using Yavsc.Server.Helpers;
2017-03-15 07:09:48 +01:00
2023-03-19 17:57:55 +00:00
[Route("api/haircut")][Authorize]
2017-03-15 07:09:48 +01:00
public class HairCutController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
private readonly ILogger _logger;
2017-03-15 07:09:48 +01:00
public HairCutController(ApplicationDbContext context,
ILoggerFactory loggerFactory)
{
_context = context;
_logger = loggerFactory.CreateLogger<HairCutController>();
}
2017-05-05 23:37:07 +02:00
// GET: api/HairCutQueriesApi
// Get the active queries for current
// user, as a client
2017-03-15 07:09:48 +01:00
public IActionResult Index()
{
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2017-03-15 07:09:48 +01:00
var now = DateTime.Now;
2017-05-24 23:36:49 +02:00
var result = _context.HairCutQueries
.Include(q => q.Prestation)
.Include(q => q.Client)
.Include(q => q.PerformerProfile)
.Include(q => q.Location)
.Where(
2017-05-15 13:36:14 +02:00
q => q.ClientId == uid
2017-05-24 23:36:49 +02:00
&& ( q.EventDate > now || q.EventDate == null )
2017-04-08 00:32:23 +02:00
&& q.Status == QueryStatus.Inserted
2017-05-24 23:36:49 +02:00
).Select(q => new HaircutQueryClientInfo(q));
2017-03-15 07:09:48 +01:00
return Ok(result);
}
2017-05-24 23:36:49 +02:00
// GET: api/HairCutQueriesApi/5
[HttpGet("{id}", Name = "GetHairCutQuery")]
public async Task<IActionResult> GetHairCutQuery([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-05-24 23:36:49 +02:00
}
HairCutQuery hairCutQuery = await _context.HairCutQueries.SingleAsync(m => m.Id == id);
if (hairCutQuery == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-05-24 23:36:49 +02:00
}
return Ok(hairCutQuery);
}
// PUT: api/HairCutQueriesApi/5
[HttpPut("{id}")]
public async Task<IActionResult> PutHairCutQuery([FromRoute] long id, [FromBody] HairCutQuery hairCutQuery)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-05-24 23:36:49 +02:00
}
if (id != hairCutQuery.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2017-05-24 23:36:49 +02:00
}
_context.Entry(hairCutQuery).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!HairCutQueryExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-05-24 23:36:49 +02:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2017-05-24 23:36:49 +02:00
}
2017-03-15 07:09:48 +01:00
[HttpPost]
2017-05-24 23:36:49 +02:00
public async Task<IActionResult> PostQuery(HairCutQuery hairCutQuery)
2017-03-15 07:09:48 +01:00
{
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2017-05-15 13:36:14 +02:00
if (!ModelState.IsValid)
{
2017-03-15 07:09:48 +01:00
return new BadRequestObjectResult(ModelState);
}
2017-05-24 23:36:49 +02:00
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-05-24 23:36:49 +02:00
}
_context.HairCutQueries.Add(hairCutQuery);
try
{
await _context.SaveChangesAsync(uid);
}
catch (DbUpdateException)
{
if (HairCutQueryExists(hairCutQuery.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2017-05-24 23:36:49 +02:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetHairCutQuery", new { id = hairCutQuery.Id }, hairCutQuery);
2017-03-15 07:09:48 +01:00
}
2017-05-12 12:15:57 +02:00
[HttpPost("createpayment/{id}")]
public async Task<IActionResult> CreatePayment(long id)
{
2017-05-24 23:36:49 +02:00
2017-05-17 01:41:52 +02:00
HairCutQuery query = await _context.HairCutQueries.Include(q => q.Client).
2017-05-15 13:36:14 +02:00
Include(q => q.Client.PostalAddress).Include(q => q.Prestation).Include(q=>q.Regularisation)
.SingleAsync(q => q.Id == id);
if (query.PaymentId!=null)
2017-05-15 15:36:01 +02:00
return new BadRequestObjectResult(new { error = "An existing payment process already exists" });
2017-05-15 13:36:14 +02:00
query.SelectedProfile = _context.BrusherProfile.Single(p => p.UserId == query.PerformerId);
2017-05-24 23:36:49 +02:00
SetExpressCheckoutResponseType payment = null;
2017-05-17 01:41:52 +02:00
try {
2017-05-24 23:36:49 +02:00
payment = Request.CreatePayment("HairCutCommand", query, "sale", _logger);
2017-05-17 01:41:52 +02:00
}
2017-05-24 23:36:49 +02:00
catch (Exception ex) {
_logger.LogError(ex.Message);
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(500);
2017-05-17 01:41:52 +02:00
}
2017-05-24 23:36:49 +02:00
if (payment==null) {
_logger.LogError("Error doing SetExpressCheckout, aborting.");
2024-02-25 18:05:10 +00:00
_logger.LogError(JsonConvert.SerializeObject(Config.PayPalSettings));
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(500);
2017-05-17 01:41:52 +02:00
}
2017-05-24 23:36:49 +02:00
switch (payment.Ack)
2017-05-15 13:36:14 +02:00
{
2017-05-24 23:36:49 +02:00
case AckCodeType.SUCCESS:
case AckCodeType.SUCCESSWITHWARNING:
2017-05-15 13:36:14 +02:00
{
2017-05-24 23:36:49 +02:00
var dbinfo = new PayPalPayment
2017-05-15 13:36:14 +02:00
{
ExecutorId = User.GetUserId(),
CreationToken = payment.Token,
State = payment.Ack.ToString()
2017-05-15 13:36:14 +02:00
};
await _context.SaveChangesAsync(User.GetUserId());
}
break;
2017-05-24 23:36:49 +02:00
default:
_logger.LogError(JsonConvert.SerializeObject(payment));
2017-05-15 13:36:14 +02:00
return new BadRequestObjectResult(payment);
}
2017-05-24 23:36:49 +02:00
return Json(new { token = payment.Token });
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteHairCutQuery([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-05-24 23:36:49 +02:00
}
HairCutQuery hairCutQuery = await _context.HairCutQueries.SingleAsync(m => m.Id == id);
if (hairCutQuery == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-05-24 23:36:49 +02:00
}
_context.HairCutQueries.Remove(hairCutQuery);
await _context.SaveChangesAsync();
return Ok(hairCutQuery);
}
private bool HairCutQueryExists(long id)
{
return _context.HairCutQueries.Count(e => e.Id == id) > 0;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
2017-05-12 12:15:57 +02:00
}
2017-03-15 07:09:48 +01:00
}
2017-04-08 00:32:23 +02:00
}