public ProfileData GetUserProfile(int id)
        {
            using var userContext = new LotteryContext();
            var result = userContext.Users
                         .Include(u => u.Payments)
                         .FirstOrDefault(user => user.Id == id)
            ;

            if (result == null)
            {
                return(null);
            }

            return(new ProfileData()
            {
                Payments = result.Payments.Select(p => new PaymentContract
                {
                    cardNumber = p.cardNumber,
                    cardType = p.cardType,
                    Id = p.Id
                }).ToList(),
                User = new UserContract()
                {
                    Address = result.Address,
                    Avatar = result.Avatar,
                    Id = result.Id,
                    Mail = result.Mail,
                    Name = result.Name,
                    Role = result.Role
                }
            });
        }
 public UserEntity UpdateProfile(UserEntity user)
 {
     using var userContext = new LotteryContext();
     userContext.Users.Update(user);
     userContext.SaveChanges();
     return(user);
 }
        public Task Save(UserEntity user)
        {
            using var userContext = new LotteryContext();
            userContext.Users.Add(user);

            userContext.SaveChanges();
            return(Task.CompletedTask);
        }
        public void SavePayment(int id, PaymentEntity paymentEntity)
        {
            using var userContext = new LotteryContext();
            var result = userContext.Users
                         .Include(user => user.Payments)
                         .FirstOrDefault(user => user.Id == id);

            if (result == null)
            {
                throw new Exception("Not found");
            }

            result.Payments.Add(paymentEntity);
            userContext.SaveChanges();
        }
        public void RemovePayment(int id, int paymentId)
        {
            using var userContext = new LotteryContext();
            var result = userContext.Users
                         .Include(user => user.Payments)
                         .FirstOrDefault(user => user.Id == id);

            if (result == null)
            {
                throw new Exception("Not found");
            }

            var payment = result.Payments.FirstOrDefault(p => p.Id == paymentId);

            if (payment == null)
            {
                throw new Exception("Not found");
            }

            result.Payments.Remove(payment);
            userContext.SaveChanges();
        }
Пример #6
0
        internal void Save(CreateLotteryEventContract contract)
        {
            using var ctx = new LotteryContext();
            var lotteryEvent = new LotteryEventEntity
            {
                Name     = contract.Name,
                DrawDate = contract.DrawDate,
                Price    = contract.Price
            };

            var charities = contract.Charities.Select(c => ctx.CharityEntity.First(entity => entity.Id == c.Id));

            foreach (var c  in charities)
            {
                lotteryEvent.LoteryCharity.Add(new CharityLotteryEntity()
                {
                    Lottery = lotteryEvent,
                    Charity = c
                });
            }
            ctx.LotteryEntity.Add(lotteryEvent);

            ctx.SaveChanges();
        }
 public UserEntity GetUserByName(string name)
 {
     using var userContext = new LotteryContext();
     return(userContext.Users.FirstOrDefault(user => user.Mail == name));
 }