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.
95 lines
2.8 KiB
95 lines
2.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Teso_API.Models;
|
|
|
|
namespace Teso_API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class ActivationHandlerController : ControllerBase
|
|
{
|
|
private readonly TESOContext _context;
|
|
|
|
public ActivationHandlerController(TESOContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/ActivationHandler/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ActivationCodes>> GetActivationCodes(string id)
|
|
{
|
|
if (ActivationCodesExists(id))
|
|
{
|
|
string guid = await _context.ActivationCodes.AsQueryable().Where(e => e.CodeGuid == id).Select(b => b.UserGuid).FirstOrDefaultAsync();
|
|
|
|
UserAuth user = await _context.UserAuths.AsQueryable().Where(e => e.UserGUID == guid).FirstOrDefaultAsync();
|
|
user.Status = "verified";
|
|
|
|
_context.Entry(user).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok("Account Verified");
|
|
}
|
|
else
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult> Activate([FromBody] int code)
|
|
{
|
|
string guid = await _context.ActivationCodes.AsQueryable().Where(e => e.Code == code).Select(b => b.UserGuid).FirstOrDefaultAsync();
|
|
UserAuth user = await _context.UserAuths.AsQueryable().Where(e => e.UserGUID == guid).FirstOrDefaultAsync();
|
|
user.Status = "verified";
|
|
|
|
_context.Entry(user).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok("Account Verified");
|
|
}
|
|
|
|
private bool ActivationCodesExists(string id)
|
|
{
|
|
if (_context.ActivationCodes.Any(e => e.CodeGuid == id))
|
|
{
|
|
DateTime generated = _context.ActivationCodes.AsQueryable().Where(e => e.CodeGuid == id).Select(e => e.DateGenerated).FirstOrDefault();
|
|
TimeSpan diff1 = DateTime.Now.Subtract(generated);
|
|
|
|
if (diff1.Hours < 48)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|