public static async Task EnsureOrderItemAsync([NotNull] this DemoDbContext ctx, Guid orderId, Guid productId, int count)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException(nameof(ctx));
            }

            var orderItem = await ctx.OrderItems.FirstOrDefaultAsync(c => c.OrderId == orderId && c.ProductId == productId);

            if (orderItem == null)
            {
                orderItem = new OrderItem {
                    OrderId = orderId, ProductId = productId
                };
                ctx.OrderItems.Add(orderItem);
            }

            orderItem.Count = count;
            await ctx.SaveChangesAsync();
        }
        public DemoDbContextTests()
        {
            dateTime     = new DateTime(2050, 1, 1);
            dateTimeMock = new Mock <IDateTime>();
            dateTimeMock.Setup(m => m.Now).Returns(dateTime);

            var options = new DbContextOptionsBuilder <DemoDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            dbContext = new DemoDbContext(options, dateTimeMock.Object);

            var user = new User
            {
                Id          = 1,
                FirstName   = "Jack",
                LastName    = "Reacher",
                CreatedDate = dateTime,
            };

            dbContext.Users.Add(user);
            dbContext.SaveChangesAsync();
        }
Example #3
0
        public async Task <IActionResult> Get(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = Constants.Routes.UserProfile)] HttpRequest req,
            ClaimsPrincipal claimsPrincipal,
            ILogger log)
        {
            if (claimsPrincipal?.Identity?.IsAuthenticated != true)
            {
                return(new UnauthorizedResult());
            }

            // Read / transform claims
            // https://docs.microsoft.com/en-us/azure/architecture/multitenant-identity/claims

            var userId   = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var userName = claimsPrincipal.FindFirst(ClaimTypes.Name)?.Value ?? claimsPrincipal.FindFirst("name")?.Value;

            var user = dbContext.Users.FirstOrDefault(u => u.PartitionKey == userId);

            if (user == null)
            {
                user = new User
                {
                    PartitionKey = userId,
                    DisplayName  = userName,
                    IsActive     = true
                };
                user.Roles.Add(new UserRole {
                    RoleName = "User"
                });

                dbContext.Users.Add(user);
                await dbContext.SaveChangesAsync();
            }

            return(user.IsActive ? (IActionResult) new OkObjectResult(FunctionHelper.ToJson(user)) : new UnauthorizedResult());
        }
Example #4
0
 public async Task <int> Add(Category entity)
 {
     _context.Categories.Add(entity);
     return(await _context.SaveChangesAsync());
 }
Example #5
0
 public virtual async Task <int> SaveAsync()
 {
     return(await dbContext.SaveChangesAsync());
 }
        public async Task Create(List <Course> courses)
        {
            await context.AddRangeAsync(courses);

            await context.SaveChangesAsync();
        }
Example #7
0
 public Task <int> SaveChanges()
 {
     return(dbContext.SaveChangesAsync());
 }
Example #8
0
        public async Task <Bill> InsertBillAsync(BillModel bill)
        {
            Bill newBill = new Bill();

            newBill.BillName    = bill.BillName;
            newBill.CreatorId   = bill.CreatorId;
            newBill.Amount      = bill.Amount;
            newBill.CreatedDate = bill.CreatedDate;
            newBill.GroupId     = bill.GroupId;
            _Context.Bill.Add(newBill);


            foreach (var person in bill.Payer)
            {
                Payer payer = new Payer();
                payer.BillId     = newBill.BillId;
                payer.PayerId    = person.Id;
                payer.PaidAmount = person.Amount;
                _Context.Payer.Add(payer);
            }


            foreach (var person in bill.SharedMember)
            {
                BillMember member = new BillMember();
                member.Billid         = newBill.BillId;
                member.SharedMemberId = person.Id;
                member.AmountToPay    = person.Amount;
                _Context.BillMember.Add(member);
            }


            for (var i = 0; i < bill.SharedMember.Count - 1; i++)
            {
                for (var j = i + 1; j < bill.SharedMember.Count; j++)
                {
                    var fExist = _Context.FriendList.SingleOrDefault(c => c.UserId == bill.SharedMember[i].Id && c.FriendId == bill.SharedMember[j].Id);
                    if (fExist == null)
                    {
                        var Exist = _Context.FriendList.SingleOrDefault(c => c.UserId == bill.SharedMember[j].Id && c.FriendId == bill.SharedMember[i].Id);
                        if (Exist == null)
                        {
                            FriendList newFriend = new FriendList
                            {
                                UserId   = bill.SharedMember[i].Id,
                                FriendId = bill.SharedMember[j].Id
                            };
                            _Context.FriendList.Add(newFriend);
                            await _Context.SaveChangesAsync();
                        }
                    }
                }
            }


            foreach (var data in bill.SettleModels)
            {
                if (data.GroupId == null)
                {
                    var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.PayerId && c.SharedMemberId == data.SharedMemberId && c.GroupId == null);

                    if (settle != null)
                    {
                        settle.TotalAmount = settle.TotalAmount + data.TotalAmount;
                        _Context.Settlement.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.SharedMemberId && c.SharedMemberId == data.PayerId && c.GroupId == null);

                        if (setle != null)
                        {
                            setle.TotalAmount = setle.TotalAmount - data.TotalAmount;
                            _Context.Settlement.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new Settlement();
                            newSettle.PayerId        = data.PayerId;
                            newSettle.SharedMemberId = data.SharedMemberId;
                            newSettle.TotalAmount    = data.TotalAmount;
                            _Context.Settlement.Add(newSettle);
                        }
                    }
                }
                else
                {
                    var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.PayerId && c.SharedMemberId == data.SharedMemberId && c.GroupId == data.GroupId);

                    if (settle != null)
                    {
                        settle.TotalAmount = settle.TotalAmount + data.TotalAmount;
                        _Context.Settlement.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.SharedMemberId && c.SharedMemberId == data.PayerId && c.GroupId == data.GroupId);

                        if (setle != null)
                        {
                            setle.TotalAmount = setle.TotalAmount - data.TotalAmount;
                            _Context.Settlement.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new Settlement();
                            newSettle.PayerId        = data.PayerId;
                            newSettle.SharedMemberId = data.SharedMemberId;
                            newSettle.TotalAmount    = data.TotalAmount;
                            newSettle.GroupId        = data.GroupId;
                            _Context.Settlement.Add(newSettle);
                        }
                    }
                }
            }
            //if (newBill.GroupId == null)
            //{
            //    foreach (var person in bill.Payer)
            //    {
            //        foreach (var member in bill.SharedMember)
            //        {
            //            if (person.Id != member.Id)
            //            {
            //                Settlement settlement = new Settlement();
            //                settlement.PayerId = member.Id;
            //                settlement.SharedMemberId = person.Id;
            //                settlement.TotalAmount = member.Amount;

            //                var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId==null);
            //                if (settle != null)
            //                {
            //                    settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
            //                    _Context.Settlement.Attach(settle);
            //                }
            //                else
            //                {
            //                    var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == null);
            //                    if (setle != null)
            //                    {
            //                        setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
            //                        _Context.Settlement.Attach(setle);
            //                    }
            //                    else
            //                    {
            //                        _Context.Settlement.Add(settlement);
            //                    }
            //                }

            //            }
            //        }
            //    }
            //}
            //else
            //{
            //    foreach (var person in bill.Payer)
            //    {
            //        foreach (var member in bill.SharedMember)
            //        {
            //            if (person.Id != member.Id)
            //            {
            //                Settlement settlement = new Settlement();
            //                settlement.GroupId = newBill.GroupId;
            //                settlement.PayerId = member.Id;
            //                settlement.SharedMemberId = person.Id;
            //                settlement.TotalAmount = member.Amount;

            //                var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == settlement.GroupId);
            //                if (settle != null)
            //                {
            //                    settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
            //                    _Context.Settlement.Attach(settle);
            //                }
            //                else
            //                {
            //                    var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == settlement.GroupId);
            //                    if (setle != null)
            //                    {
            //                        setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
            //                        _Context.Settlement.Attach(setle);
            //                    }
            //                    else
            //                    {
            //                        _Context.Settlement.Add(settlement);
            //                    }
            //                }
            //            }
            //        }
            //    }

            //}
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertBillAsync)}: " + exp.Message);
            }

            return(newBill);
        }
Example #9
0
        public async Task <int> Add(Product entity)
        {
            await _context.AddAsync(entity);

            return(await _context.SaveChangesAsync());
        }
Example #10
0
 public async Task SaveChanges()
 {
     await _context.SaveChangesAsync();
 }
        public async Task AddCity(CityDataModel city)
        {
            await _demoDbContext.CityMaster.AddAsync(city);

            await _demoDbContext.SaveChangesAsync();
        }
 public async Task Create(Tent tent)
 {
     context.Tents.Add(tent);
     await context.SaveChangesAsync();
 }
Example #13
0
        public async Task <Bill> InsertBillAsync(BillModel bill)
        {
            Bill newBill = new Bill();

            newBill.BillName    = bill.BillName;
            newBill.CreatorId   = bill.CreatorId;
            newBill.CreatedDate = bill.CreatedDate;
            newBill.GroupId     = bill.GroupId;
            _Context.Bill.Add(newBill);


            foreach (var person in bill.Payer)
            {
                Payer payer = new Payer();
                payer.BillId     = newBill.BillId;
                payer.PayerId    = person.Id;
                payer.PaidAmount = person.Amount;
                _Context.Payer.Add(payer);
            }


            foreach (var person in bill.SharedMember)
            {
                BillMember member = new BillMember();
                member.Billid         = newBill.BillId;
                member.SharedMemberId = person.Id;
                member.AmountToPay    = person.Amount;
                _Context.BillMember.Add(member);
            }


            if (newBill.GroupId == null)
            {
                foreach (var person in bill.Payer)
                {
                    foreach (var member in bill.SharedMember)
                    {
                        if (person.Id != member.Id)
                        {
                            Settlement settlement = new Settlement();
                            settlement.PayerId        = member.Id;
                            settlement.SharedMemberId = person.Id;
                            settlement.TotalAmount    = member.Amount;

                            var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == null);

                            if (settle != null)
                            {
                                settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
                                _Context.Settlement.Attach(settle);
                            }
                            else
                            {
                                var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == null);

                                if (setle != null)
                                {
                                    setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
                                    _Context.Settlement.Attach(setle);
                                }
                                else
                                {
                                    _Context.Settlement.Add(settlement);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var person in bill.Payer)
                {
                    foreach (var member in bill.SharedMember)
                    {
                        if (person.Id != member.Id)
                        {
                            Settlement settlement = new Settlement();
                            settlement.GroupId        = newBill.GroupId;
                            settlement.PayerId        = member.Id;
                            settlement.SharedMemberId = person.Id;
                            settlement.TotalAmount    = member.Amount;

                            var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == settlement.GroupId);

                            if (settle != null)
                            {
                                settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
                                _Context.Settlement.Attach(settle);
                            }
                            else
                            {
                                var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == settlement.GroupId);

                                if (setle != null)
                                {
                                    setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
                                    _Context.Settlement.Attach(setle);
                                }
                                else
                                {
                                    _Context.Settlement.Add(settlement);
                                }
                            }
                        }
                    }
                }
            }
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertBillAsync)}: " + exp.Message);
            }

            return(newBill);
        }
        public async Task <Group> InsertGroupAsync(GroupModel group)
        {
            Group grp = new Group();

            grp.GroupName   = group.GroupName;
            grp.CreatedDate = group.CreatedDate;
            grp.CreatorId   = group.CreatorId;
            _Context.Group.Add(grp);

            foreach (var member in group.Members)
            {
                GroupMember grpMember = new GroupMember();
                grpMember.Group_Id = grp.GroupId;
                grpMember.User_Id  = member;
                _Context.GroupMember.Add(grpMember);
            }

            for (var i = 0; i < group.Members.Count - 1; i++)
            {
                for (var j = i + 1; j < group.Members.Count; j++)
                {
                    var fExist = _Context.FriendList.SingleOrDefault(c => c.UserId == group.Members[i] && c.FriendId == group.Members[j]);
                    if (fExist == null)
                    {
                        var Exist = _Context.FriendList.SingleOrDefault(c => c.UserId == group.Members[j] && c.FriendId == group.Members[i]);
                        if (Exist == null)
                        {
                            FriendList newFriend = new FriendList
                            {
                                UserId   = group.Members[i],
                                FriendId = group.Members[j]
                            };
                            _Context.FriendList.Add(newFriend);
                            await _Context.SaveChangesAsync();
                        }
                    }
                }
            }

            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertGroupAsync)}: " + exp.Message);
            }

            return(grp);
        }
Example #15
0
 public async Task <bool> SaveAsync()
 {
     return(await _context.SaveChangesAsync() >= 0);
 }
Example #16
0
        public async Task <Transactions> RecordPaymentAsync(Transactions payment)
        {
            _Context.Transactions.Add(payment);

            if (payment.GroupId == null)
            {
                var settleData = _Context.Settlement.SingleOrDefault(c => c.PayerId == payment.TransPayersId && c.SharedMemberId == payment.TransReceiversId && c.GroupId == null);
                if (settleData != null)
                {
                    settleData.TotalAmount = settleData.TotalAmount - payment.PaidAmount;
                    _Context.Settlement.Attach(settleData);
                }
                else
                {
                    //settle.TotalAmount = payment.PaidAmount;
                    //_Context.Settlement.Add(settle);

                    var set = _Context.Settlement.SingleOrDefault(c => c.PayerId == payment.TransReceiversId && c.SharedMemberId == payment.TransPayersId && c.GroupId == null);
                    if (set != null)
                    {
                        set.TotalAmount = set.TotalAmount + payment.PaidAmount;
                        _Context.Settlement.Attach(set);
                    }
                    else
                    {
                        Settlement settle = new Settlement();
                        settle.PayerId        = payment.TransReceiversId;
                        settle.SharedMemberId = payment.TransPayersId;
                        settle.TotalAmount    = payment.PaidAmount;
                        _Context.Settlement.Add(settle);
                    }
                }
            }
            else
            {
                var settleData = _Context.Settlement.SingleOrDefault(c => c.PayerId == payment.TransPayersId && c.SharedMemberId == payment.TransReceiversId && c.GroupId == payment.GroupId);
                if (settleData != null)
                {
                    settleData.TotalAmount = settleData.TotalAmount - payment.PaidAmount;
                    _Context.Settlement.Attach(settleData);
                }
                else
                {
                    var set = _Context.Settlement.SingleOrDefault(c => c.PayerId == payment.TransReceiversId && c.SharedMemberId == payment.TransPayersId && c.GroupId == payment.GroupId);
                    if (set != null)
                    {
                        set.TotalAmount = set.TotalAmount + payment.PaidAmount;
                        _Context.Settlement.Attach(set);
                    }
                    else
                    {
                        Settlement settle = new Settlement();
                        settle.SharedMemberId = payment.TransPayersId;
                        settle.PayerId        = payment.TransReceiversId;
                        settle.GroupId        = payment.GroupId;
                        settle.TotalAmount    = payment.PaidAmount;
                        _Context.Settlement.Add(settle);
                    }
                }
            }
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(RecordPaymentAsync)}: " + exp.Message);
            }

            return(payment);
        }
        public async Task <FriendList> InsertFriendAsync(int id, string userName, string email)
        {
            var userExist = _Context.User.SingleOrDefault(c => c.UserName == userName && c.Email == email);

            if (userExist != null)
            {
                var member = _Context.FriendList.SingleOrDefault(c => c.UserId == id && c.FriendId == userExist.UserId);
                if (member == null)
                {
                    var memExist = _Context.FriendList.SingleOrDefault(c => c.UserId == userExist.UserId && c.FriendId == id);
                    if (memExist == null)
                    {
                        FriendList newFriend = new FriendList
                        {
                            UserId   = id,
                            FriendId = userExist.UserId
                        };
                        _Context.FriendList.Add(newFriend);
                        await _Context.SaveChangesAsync();

                        return(newFriend);
                    }
                    else
                    {
                        _Context.FriendList.Attach(memExist);
                        await _Context.SaveChangesAsync();

                        return(memExist);
                    }
                }
                else
                {
                    _Context.FriendList.Attach(member);
                    await _Context.SaveChangesAsync();

                    return(member);
                }
            }
            else
            {
                FriendList notExist = new FriendList
                {
                    UserId   = 0,
                    FriendId = 0
                };
                return(notExist);
            }
            //var member = _Context.FriendList.SingleOrDefault(c => c.UserId == Userid && c.FriendId == Friendid);

            //if(member== null)
            //{
            //    var exist = _Context.FriendList.SingleOrDefault(c => c.FriendId == Userid && c.UserId == Friendid);
            //    if (exist == null)
            //    {
            //        FriendList newFriend = new FriendList
            //        {
            //            UserId = Userid,
            //            FriendId = Friendid
            //        };

            //        _Context.FriendList.Add(newFriend);
            //        await _Context.SaveChangesAsync();
            //        return newFriend;
            //    }
            //    else
            //    {
            //        _Context.FriendList.Attach(exist);
            //        await _Context.SaveChangesAsync();
            //        return  exist;
            //    }
            //}

            //else
            //{
            //    _Context.FriendList.Attach(member);
            //    await _Context.SaveChangesAsync();
            //    return member;
            //}
        }