Example #1
0
        public async Task <IActionResult> PutPromo(int id, Promo promo)
        {
            if (id != promo.Id)
            {
                return(BadRequest());
            }

            _context.Entry(promo).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PromoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public async Task <ActionResult> ChargeOff([FromHeader(Name = "Authorization")] string JWT, Charge charge)
        {
            var admin = RightCredentials(charge.AdminId);

            if (admin.Result == null || !admin.Result.IsAdmin)
            {
                return(BadRequest("Вы не админ!"));
            }

            var client = _context.Cards.Where(c => c.CardCode == charge.CardCode).FirstOrDefaultAsync().Result;

            if (client == null)
            {
                return(NotFound("Клиент не найден"));
            }

            if (client.BonusQuantity < charge.BonusQuantity)
            {
                return new ContentResult()
                       {
                           StatusCode = (int)HttpStatusCode.MethodNotAllowed,
                           Content    = "Недостаточно бонусов на счете",
                       }
            }
            ;

            client.BonusQuantity        -= charge.BonusQuantity;
            _context.Entry(client).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IActionResult> PutClient([FromHeader(Name = "Authorization")] string JWT, int id, ClientRequest client)
        {
            var clientEntity = RightCredentials(id).Result;

            if (id != client.Id || clientEntity == null)
            {
                return(BadRequest());
            }

            clientEntity.Birthdate = client.Birthdate;
            clientEntity.Name      = client.Name;
            clientEntity.Surname   = client.Surname;
            clientEntity.Phone     = client.Phone;
            clientEntity.Email     = client.Email;

            _context.Entry(clientEntity).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #4
0
        public async Task <ActionResult> AddDevice(int clientId, string token)
        {
            var device = new Device()
            {
                ClientId = clientId,
                Token    = token
            };

            var deviceInDb = await _context.Devices.Where(d => d.Token == token).FirstOrDefaultAsync();

            if (deviceInDb != null)
            {
                _context.Remove(deviceInDb);
            }

            _context.Devices.Add(device);
            await _context.SaveChangesAsync();

            return(Ok());
        }
Example #5
0
        //[Authorize(Roles = Models.Client.Role)]
        public async Task <IActionResult> PutCard([FromHeader(Name = "Authorization")] string JWT, CardRequest card, int clientId)
        {
            var client = await RightCredentials(clientId);

            if (client == null)
            {
                return(BadRequest());
            }

            var cardEntity = client.Card;

            if (cardEntity == null)
            {
                return(NotFound());
            }

            cardEntity.StartDate     = card.StartDate;
            cardEntity.EndDate       = card.EndDate;
            cardEntity.BonusQuantity = card.BonusQuantity;

            _context.Entry(cardEntity).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CardExists(cardEntity.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #6
0
        public async Task <IActionResult> Token(string number, int code)
        {
            var identity = GetIdentity(number, code);

            if (identity == null)
            {
                return(BadRequest(new { errorText = "Invalid username or code." }));
            }

            var now = DateTime.UtcNow;
            // создаем JWT-токен
            var jwt = new JwtSecurityToken(
                issuer: AuthOptions.ISSUER,
                audience: AuthOptions.AUDIENCE,
                notBefore: now,
                claims: identity.Claims,
                expires: now.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME)),
                signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);



            var client = await _context.Clients.Where(c => c.Phone == number).FirstOrDefaultAsync();

            client.AuthCode          = null;
            client.AuthCodeDeathTime = null;
            await _context.SaveChangesAsync();

            var response = new
            {
                access_token = encodedJwt,
                username     = identity.Name,
                user_id      = client.Id
            };

            return(Ok(response));
        }