You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.7 KiB
110 lines
3.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Teso_API.Models;
|
|
|
|
namespace Teso_API.Controllers
|
|
{
|
|
[AllowAnonymous, Route("monthly-desires")]
|
|
[ApiController]
|
|
public class DesireHeadsController : ControllerBase
|
|
{
|
|
private readonly TESOContext _context;
|
|
|
|
public DesireHeadsController(TESOContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[Authorize]
|
|
[Route("check-status"),HttpGet]
|
|
public async Task<ActionResult> GetDesireHeads()
|
|
{
|
|
var accessToken = Request.Headers[HeaderNames.Authorization];
|
|
string token = accessToken;
|
|
token = token.Substring(6).Trim();
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
|
|
string userID = jwtToken.Claims.First(claim => claim.Type == "userGUID").Value;
|
|
|
|
if (DateTime.Now.Day > 24)
|
|
{
|
|
DateTime date = DateTime.Now.AddMonths(1);
|
|
bool result = _context.DesireHeads.Any(e => e.UserGuid == userID && e.Month.Month == date.Month && e.Month.Year == date.Year);
|
|
|
|
if (result)
|
|
{
|
|
return Ok("submitted");
|
|
}
|
|
else
|
|
{
|
|
return Ok("not submitted");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return Ok("not due");
|
|
}
|
|
}
|
|
|
|
[Authorize]
|
|
[Route("submit-newdesire"),HttpPost]
|
|
public async Task<ActionResult> PostDesireHead(List<DesiredItem> items)
|
|
{
|
|
var accessToken = Request.Headers[HeaderNames.Authorization];
|
|
string token = accessToken;
|
|
token = token.Substring(6).Trim();
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
|
|
string userID = jwtToken.Claims.First(claim => claim.Type == "userGUID").Value;
|
|
|
|
if (DateTime.Now.Day > 24)
|
|
{
|
|
List<DesireDetail> desireDetails = new List<DesireDetail>();
|
|
DesireHead desireHead = new DesireHead();
|
|
desireHead.DesireGuid = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 10000000) + userID;
|
|
desireHead.UserGuid = userID;
|
|
desireHead.Month = DateTime.Now.AddMonths(1);
|
|
int i = 1;
|
|
foreach (DesiredItem item in items)
|
|
{
|
|
DesireDetail desire = new DesireDetail();
|
|
desire.CountId = DateTime.Now.ToString("yyyyMMddHHmmssfff") + userID + i;
|
|
desire.ProductId = item.productID;
|
|
desire.Enlisted = item.enlisted == "true" ? true : false;
|
|
desire.DesireGuid = desireHead.DesireGuid;
|
|
desire.Category = item.category;
|
|
|
|
desireDetails.Add(desire);
|
|
i++;
|
|
}
|
|
|
|
_context.DesireDetails.AddRange(desireDetails);
|
|
|
|
_context.DesireHeads.Add(desireHead);
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateException)
|
|
{
|
|
return Conflict();
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
else
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|