ENEI2019-Public/api/Controllers/CromosController.cs

58 lines
1.8 KiB
C#
Raw Normal View History

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
{
private readonly DataContext context;
private readonly IMapper _mapper;
public CromosController(DataContext context, IMapper mapper)
{
this.context = context;
_mapper = mapper;
}
// GET api/cromos/QR
// GET cromos do user QR
2019-03-16 19:17:19 +00:00
[HttpGet("{QR}")]
2019-03-14 18:10:59 +00:00
public async Task<List<Cromos>> GetCromos(string QR)
{
2019-03-16 16:32:37 +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-14 18:10:59 +00:00
var allCromos = await context.Cromos.ToListAsync();
List<Cromos> rList = new List<Cromos>();
allCromos.ForEach(delegate(Cromos c){
2019-03-16 16:32:37 +00:00
for(int i=0;i<usrCromos.Length;i++){
if(Int32.Parse(usrCromos[i])==c.Id){
2019-03-14 18:10:59 +00:00
Cromos toAdd = new Cromos{Id = c.Id,Nome=c.Nome,DescMostrar=c.DescUnlocked,QRCode=c.QRCode,img=c.img};
rList.Add(toAdd);
}else{ //user NAO tem o cromo
Cromos toAdd = new Cromos{Id = c.Id,Nome=c.Nome,DescMostrar=c.DescLocked,QRCode=c.QRCode,img=c.img};
rList.Add(toAdd);
}
2019-03-16 16:32:37 +00:00
}
2019-03-14 18:10:59 +00:00
});
return rList;
}
}
}