Пример #1
0
        public virtual async Task <TModel> Insert(TInsert request)
        {
            var entity = _mapper.Map <TDatabase>(request);

            _context.Set <TDatabase>().Add(entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <TModel>(entity));
        }
Пример #2
0
        public async Task <bool> Delete(int id)
        {
            var entity = await _context.Set <Rezervacija>().FindAsync(id);

            try
            {
                _context.Set <Rezervacija>().Remove(entity);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public async Task <KorisnikResponse> UpdateLicneInformacije(int ID, KorisnikUpdateInfoRequest request)
        {
            var entity = await _context.Set <Korisnik>().FindAsync(ID);

            var user = await _userManager.FindByEmailAsync(entity.Email);

            if (!string.IsNullOrEmpty(request.Ime) && user.Ime != request.Ime)
            {
                user.Ime = request.Ime;
            }

            if (!string.IsNullOrEmpty(request.Prezime) && user.Prezime != request.Prezime)
            {
                user.Prezime = request.Prezime;
            }

            if (!string.IsNullOrEmpty(request.Email) && user.Email != request.Email)
            {
                var token = await _userManager.GenerateChangeEmailTokenAsync(user, request.Email);

                await _userManager.ChangeEmailAsync(user, request.Email, token);
            }

            if (request.GradID != null && user.GradID != request.GradID)
            {
                user.GradID = request.GradID;
            }

            await _context.SaveChangesAsync();

            return(_mapper.Map <KorisnikResponse>(entity));
        }
        public async Task <bool> OcistiKorpu(string KorpaID)
        {
            var stavke = _context.KorpaStavke.Where(korpa => korpa.KorpaID == KorpaID);

            if (stavke != null)
            {
                _context.KorpaStavke.RemoveRange(stavke);
                await _context.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
        public async Task <NarudzbaResponse> Insert(int korisnikID, NarudzbaInsertRequest request)
        {
            var nacinPlacanja = new NacinPlacanja()
            {
                Naziv = "Pouzećem"
            };

            _context.NaciniPlacanja.Add(nacinPlacanja);
            await _context.SaveChangesAsync();

            var entity = _mapper.Map <Narudzba>(request);

            entity.KorisnikID      = korisnikID;
            entity.NacinPlacanjaID = nacinPlacanja.ID;

            _context.Set <Narudzba>().Add(entity);
            await _context.SaveChangesAsync();

            foreach (var detalj in request.NarudzbaDetalji)
            {
                detalj.NarudzbaID = entity.ID;
            }
            var detalji = _mapper.Map <List <NarudzbaDetalji> >(request.NarudzbaDetalji);

            _context.NarudzbaDetalji.AddRange(detalji);
            await _context.SaveChangesAsync();

            double suma    = 0;
            var    message = "Poštovana/i, " + _context.Users.Find(entity.KorisnikID).Ime + " " + _context.Users.Find(entity.KorisnikID).Prezime +
                             ", uspješno ste naručili vaša jela: \n";

            message += "\n";
            foreach (var x in detalji)
            {
                message += _context.Jela.Find(x.JeloID).Naziv + "\t" + " - " + x.Cijena + " KM" + "(" + x.Kolicina + " kom), \n";
                suma    += x.Kolicina * x.Cijena;
            }

            message += "Ukupna cijena narudžbe: " + suma + " KM \n";
            message += "Lijep pozdrav!";


            await _emailSender.SendEmailAsync(new string[] { entity.Korisnik.Email }, "Potvrda narudžbe", message);

            return(_mapper.Map <NarudzbaResponse>(entity));
        }
Пример #6
0
        public async Task <bool> Like(int JeloID, int KorisnikID)
        {
            var entity = new Like
            {
                JeloID     = JeloID,
                KorisnikID = KorisnikID,
                Datum      = DateTime.Now
            };

            try
            {
                await _context.AddAsync(entity);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #7
0
        public async Task <AuthenticationResult> RefreshTokenAsync(RefreshTokenRequest request)
        {
            var validatedToken = GetPrincipalFromToken(request.Token);

            if (validatedToken == null)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "Invalid Token" }
                });
            }

            var expiryDateUnix =
                long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This token hasn't expired yet" }
                });
            }

            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token == request.RefreshToken);

            if (storedRefreshToken == null)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token does not exist" }
                });
            }

            if (DateTime.UtcNow > storedRefreshToken.ExpiryDate)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has expired" }
                });
            }

            if (storedRefreshToken.Invalidated)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has been invalidated" }
                });
            }

            if (storedRefreshToken.Used)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has been used" }
                });
            }

            if (storedRefreshToken.JwtId != jti)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token does not match this JWT" }
                });
            }

            storedRefreshToken.Used = true;
            _context.RefreshTokens.Update(storedRefreshToken);
            await _context.SaveChangesAsync();

            var user = await _userManager.FindByIdAsync(validatedToken.Claims.Single(x => x.Type == "id").Value);

            return(await GenerateAuthenticationResultForUserAsync(user));
        }