Ejemplo n.º 1
0
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] Checkin checkin)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != checkin.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] RepetitionType repetitionType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != repetitionType.Id)
            {
                return(BadRequest());
            }

            repetitionType.ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity).FindFirst(ClaimTypes.Sid).Value);
            repetitionType.ModifiedDate   = DateTime.Now;

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

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

            return(Ok(repetitionType));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Login([FromBody] User user)
        {
            int?promotionPromoterId = null;

            if (user.Email == null || user.Password == null)
            {
                return(Unauthorized());
            }
            var myUser = await _context.User.FirstOrDefaultAsync(u => u.Email == user.Email);

            if (!PasswordHelper.ValidatePassword(user.Password, myUser.Password))
            {
                return(Unauthorized());
            }

            if (user.Username == null)
            {
                return(Ok(new { token = GenerateToken(myUser.Email, myUser.Id, null) }));
            }
            {
                var promoter = await _context.Promoter.Include(a => a.PromotionPromoter).ThenInclude(b => b.Promotion).FirstOrDefaultAsync(u => u.Username == user.Username);

                if (promoter == null)
                {
                    return(Unauthorized());
                }

                var promotionPromoters = promoter?.PromotionPromoter;

                if (promotionPromoters != null)
                {
                    foreach (var p in promotionPromoters)
                    {
                        if (p.Promotion.Date != DateTime.Today)
                        {
                            continue;
                        }
                        promotionPromoterId = p.Id;
                        _context.Checkin.Add(new Checkin
                        {
                            PromotionPromoterId = p.Id,
                            Latitude            = user.Latitude,
                            Longitude           = user.Longitude,
                            ModifiedDate        = DateTime.Now,
                            ModifiedUserId      = myUser.Id,
                            IsActive            = true
                        });
                        await _context.SaveChangesAsync();

                        break;
                    }
                }

                if (promotionPromoterId == null)
                {
                    return(BadRequest("No promotions for this user today"));
                }
            }

            return(Ok(new { token = GenerateToken(myUser.Email, myUser.Id, promotionPromoterId) }));
        }
        public async Task <IActionResult> Post([FromBody] Promotion promotion)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (var context = _context)
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        promotion.ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity)
                                                                   .FindFirst(ClaimTypes.Sid).Value);
                        promotion.ModifiedDate = DateTime.Now;

                        context.Promotion.Add(promotion);

                        await _context.SaveChangesAsync();

                        var promotionId = promotion.Id;

                        foreach (var id in promotion.ProductIds)
                        {
                            context.PromotionProduct.Add(new PromotionProduct
                            {
                                ProductId      = id,
                                PromotionId    = promotionId,
                                ModifiedDate   = DateTime.Now,
                                ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity)
                                                                 .FindFirst(ClaimTypes.Sid).Value),
                                IsActive = true
                            });
                        }

                        foreach (var id in promotion.PromoterIds)
                        {
                            context.PromotionPromoter.Add(new PromotionPromoter
                            {
                                PromoterId     = id,
                                PromotionId    = promotionId,
                                ModifiedDate   = DateTime.Now,
                                ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity)
                                                                 .FindFirst(ClaimTypes.Sid).Value),
                                IsActive = true
                            });
                        }

                        await context.SaveChangesAsync();

                        // Commit transaction if all commands succeed, transaction will auto-rollback
                        // when disposed if either commands fails
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        return(BadRequest(e.Message));
                    }
                }
            }
            return(CreatedAtAction("Get", new { id = promotion.Id }, promotion));
        }