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 CromosController : ControllerBase
|
2019-04-11 05:35:49 +01:00
|
|
|
{ private readonly IUsersRepository _repo;
|
2019-03-14 18:10:59 +00:00
|
|
|
private readonly DataContext context;
|
|
|
|
private readonly IMapper _mapper;
|
2019-04-11 05:35:49 +01:00
|
|
|
public CromosController(IUsersRepository repo,DataContext context, IMapper mapper)
|
2019-03-14 18:10:59 +00:00
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
_mapper = mapper;
|
2019-04-11 05:35:49 +01:00
|
|
|
_repo = repo;
|
2019-03-14 18:10:59 +00:00
|
|
|
}
|
2019-03-19 03:03:56 +00:00
|
|
|
|
2019-03-14 18:10:59 +00:00
|
|
|
// GET api/cromos/QR
|
|
|
|
// GET cromos do user QR
|
2019-04-11 05:35:49 +01:00
|
|
|
|
2019-03-16 19:17:19 +00:00
|
|
|
[HttpGet("{QR}")]
|
2019-04-11 05:35:49 +01:00
|
|
|
|
2019-03-19 01:25:54 +00:00
|
|
|
public async Task<IActionResult> GetCromos(string QR)
|
2019-03-14 18:10:59 +00:00
|
|
|
{
|
2019-03-19 03:03:56 +00:00
|
|
|
int soma = 0;
|
2019-04-11 05:35:49 +01:00
|
|
|
int somaCTF=0;
|
2019-03-19 03:03:56 +00:00
|
|
|
var usr = await context.Users.FirstOrDefaultAsync(u => u.QRcode == QR);
|
2019-03-16 19:17:19 +00:00
|
|
|
string[] usrCromos = usr.cromos.Substring(1).Split(",");
|
|
|
|
Console.WriteLine(usrCromos[0]);
|
2019-03-19 00:35:35 +00:00
|
|
|
var allCromos = await context.Cromos.ToListAsync();
|
2019-03-14 18:10:59 +00:00
|
|
|
|
|
|
|
List<Cromos> rList = new List<Cromos>();
|
2019-03-19 00:26:20 +00:00
|
|
|
|
2019-03-19 03:03:56 +00:00
|
|
|
allCromos.ForEach(delegate (Cromos c)
|
|
|
|
{
|
|
|
|
Boolean found = false;
|
|
|
|
for (int i = 0; i < usrCromos.Length; i++)
|
|
|
|
{
|
2019-03-19 00:26:20 +00:00
|
|
|
|
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
if (Int32.Parse(usrCromos[i]) == c.Id)
|
2019-03-19 03:03:56 +00:00
|
|
|
{
|
2019-04-04 01:46:01 +01:00
|
|
|
|
2019-03-19 03:03:56 +00:00
|
|
|
Cromos toAdd = new Cromos { Id = c.Id, Nome = c.Nome, DescMostrar = c.DescUnlocked, QRCode = c.QRCode, img = c.img, unlocked = true, websiteCromo = c.websiteCromo, pontos = c.pontos, logo = c.logo };
|
2019-04-04 01:38:44 +01:00
|
|
|
|
2019-04-04 01:46:01 +01:00
|
|
|
var d = rList.Find(x => x.Id == c.Id);
|
|
|
|
|
|
|
|
if (d != null)
|
2019-04-04 01:38:44 +01:00
|
|
|
continue;
|
2019-04-04 01:46:01 +01:00
|
|
|
else
|
|
|
|
{
|
2019-04-11 05:35:49 +01:00
|
|
|
if(c.Id == 4 || c.Id==5 || c.Id==6 || c.Id==7 || c.Id==8 || c.Id==9 || c.Id==10 || c.Id==11 || c.Id==12 || c.Id==13 || c.Id==14 || c.Id==16){
|
|
|
|
somaCTF += c.pontos;
|
|
|
|
}
|
2019-04-04 01:46:01 +01:00
|
|
|
soma += c.pontos;
|
|
|
|
rList.Add(toAdd);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:03:56 +00:00
|
|
|
|
2019-03-14 18:10:59 +00:00
|
|
|
}
|
2019-03-19 03:03:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Cromos toAdd = new Cromos { Id = c.Id, Nome = c.Nome, DescMostrar = c.DescLocked, QRCode = c.QRCode, img = c.img, unlocked = false, websiteCromo = c.websiteCromo, pontos = c.pontos };
|
|
|
|
rList.Add(toAdd);
|
|
|
|
|
2019-03-16 16:32:37 +00:00
|
|
|
}
|
2019-03-14 18:10:59 +00:00
|
|
|
});
|
2019-03-19 01:25:54 +00:00
|
|
|
|
2019-03-19 03:03:56 +00:00
|
|
|
cromosToReturn a = new cromosToReturn();
|
|
|
|
a.cromos = rList;
|
|
|
|
a.pontuacao = soma;
|
2019-04-11 05:35:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
var userFromRepo = await _repo.GetUser(usr.Id);
|
|
|
|
|
|
|
|
userFromRepo.food = somaCTF;
|
|
|
|
userFromRepo.drinks = soma;
|
|
|
|
|
|
|
|
|
|
|
|
if (await _repo.SaveAll()){}
|
|
|
|
|
|
|
|
|
2019-03-19 01:25:54 +00:00
|
|
|
return Ok(a);
|
2019-03-14 18:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|