2019-03-14 18:10:59 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using api.Data;
|
|
|
|
using api.Dtos;
|
|
|
|
using api.Models;
|
|
|
|
using AutoMapper;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace api.Controllers
|
|
|
|
{
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
[ApiController]
|
|
|
|
public class ScanController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly DataContext context;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
public ScanController(DataContext context, IMapper mapper)
|
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
_mapper = mapper;
|
|
|
|
}
|
2019-04-04 01:46:01 +01:00
|
|
|
|
2019-03-14 18:10:59 +00:00
|
|
|
// PSOT api/scan
|
|
|
|
// POST scan de QR code
|
|
|
|
[HttpPost]
|
2019-03-26 04:20:40 +00:00
|
|
|
public async Task<IActionResult> doScan(QRToScan ScanData)
|
2019-03-14 18:10:59 +00:00
|
|
|
{
|
2019-04-04 01:46:01 +01:00
|
|
|
User usr = await context.Users.FirstOrDefaultAsync(b => b.QRcode == ScanData.UserQR);
|
2019-03-14 18:10:59 +00:00
|
|
|
var allUsers = await context.Users.ToListAsync();
|
|
|
|
var allCromos = await context.Cromos.ToListAsync();
|
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
var userAProcurar = await context.Users.FirstOrDefaultAsync(c => c.QRcode == ScanData.ScanQR);
|
2019-03-14 18:10:59 +00:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
ScanReturn toReturn = new ScanReturn { tipo = -1 };
|
2019-03-14 18:10:59 +00:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
if (userAProcurar != null)
|
|
|
|
{
|
2019-04-04 01:24:06 +01:00
|
|
|
|
2019-03-16 19:17:19 +00:00
|
|
|
UserForListDto ru = new UserForListDto();
|
2019-04-04 01:46:01 +01:00
|
|
|
_mapper.Map(userAProcurar, ru);
|
2019-03-16 19:17:19 +00:00
|
|
|
toReturn.user = ru;
|
2019-04-04 01:46:01 +01:00
|
|
|
toReturn.tipo = 1;
|
2019-03-26 04:20:40 +00:00
|
|
|
return Ok(toReturn);
|
2019-04-04 01:24:06 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-04 01:51:17 +01:00
|
|
|
bool repetido = false;
|
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
allCromos.ForEach(delegate (Cromos c)
|
|
|
|
{
|
2019-04-04 01:51:17 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
if (c.QRCode == ScanData.ScanQR)
|
|
|
|
{
|
|
|
|
toReturn.tipo = 0;
|
2019-04-04 01:51:17 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
string[] usrCromos = usr.cromos.Substring(1).Split(",");
|
2019-04-04 01:24:06 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
foreach (string cromo in usrCromos)
|
|
|
|
{
|
2019-04-04 01:51:17 +01:00
|
|
|
Console.WriteLine("cromo: "+cromo);
|
2019-04-04 01:54:55 +01:00
|
|
|
if (c.Id == Int32.Parse(cromo))
|
2019-04-04 01:46:01 +01:00
|
|
|
{
|
|
|
|
repetido = true;
|
2019-04-04 01:51:17 +01:00
|
|
|
Console.WriteLine("cromo repetido");
|
2019-04-04 01:46:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 01:24:06 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
if (!repetido)
|
|
|
|
{
|
|
|
|
usr.cromos = usr.cromos + "," + c.Id;
|
|
|
|
context.Users.Update(usr);
|
|
|
|
context.SaveChanges();
|
|
|
|
toReturn.resp = "Cromo Adicionado!";
|
|
|
|
}
|
2019-04-04 01:24:06 +01:00
|
|
|
|
2019-03-14 18:10:59 +00:00
|
|
|
}
|
|
|
|
});
|
2019-04-04 01:46:01 +01:00
|
|
|
|
2019-04-04 01:51:17 +01:00
|
|
|
if (repetido)
|
|
|
|
{
|
|
|
|
return Unauthorized();
|
|
|
|
}
|
2019-03-26 04:20:40 +00:00
|
|
|
return Ok(toReturn);
|
2019-03-14 18:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|