예제 #1
0
        public void Finish()
        {
            _batchLogService.StopBatch(_batchId);

            _context.SaveChanges();
            _context.Dispose();
        }
 /// <summary>
 /// Delete record of the barcode in the repository.
 /// </summary>
 /// <param name="barcode">Barcode model.</param>
 public void Delete(BarcodeDB barcode)
 {
     if (barcode != null)
     {
         _wasteContext.Barcodes.Remove(barcode);
     }
     _wasteContext.SaveChanges();
 }
예제 #3
0
        //TODO: Trochę nie podoba mi sie sposób implementacji Delete. Potrzebne inne ropozytorium
        public override void Delete(Guid id)
        {
            var entity = this.Get(id);

            entity.IsEnabled  = false;
            entity.UpdateDate = DateTime.Now;

            _context.SaveChanges();
        }
        public void Add(UserDB user)
        {
            if (user.Id != null)
            {
                throw new ArgumentException("Cannot Add User with Id different from null.");
            }

            user.Created = DateTime.UtcNow;
            using (var db = new WasteContext())
            {
                db.Users.Add(user);
                db.SaveChanges();
            }
        }
        public void Update(UserDB user)
        {
            using (var db = new WasteContext())
            {
                var userInDB = db.Users.Find(user.Id);

                user.Created = userInDB.Created;

                db.Entry(userInDB).CurrentValues.SetValues(user);

                userInDB.Modified = DateTime.UtcNow;

                db.SaveChanges();
            }
        }
예제 #6
0
 public static void WorkWithEF()
 {
     using (WasteContext dbContext = new WasteContext())
     {
         var userDB = new UserDB()
         {
             UserName             = "******",
             Email                = "*****@*****.**",
             EmailConfirmed       = true,
             PasswordHash         = "qwerty",
             SecurityStamp        = "Stamp",
             PhoneNumber          = "+375255024800",
             PhoneNumberConfirmed = true,
             TwoFactorEnabled     = false,
             LockoutEndDateUtc    = null,
             Created              = DateTime.UtcNow,
             Modified             = null
         };
         dbContext.Users.Add(userDB);
         dbContext.SaveChanges();
         Console.WriteLine("Done!");
     }
 }
        public void Delete(UserDB user)
        {
            if (user.Id != null)
            {
                using (var db = new WasteContext())
                {
                    var result = db.Users.Where(f => f.Id == user.Id).FirstOrDefault();

                    if (result != null)
                    {
                        db.Users.Remove(result);
                        db.SaveChanges();
                    }
                    else
                    {
                        throw new ArgumentException($"The User cannot be deleted because User with Id = {user.Id} doesn't exist.");
                    }
                }
            }
            else
            {
                throw new ArgumentException("Cannot delete User with Id = null.");
            }
        }
 /// <summary>
 /// Add new barcode in the repository.
 /// </summary>
 /// <param name="barcode">New barcode to add.</param>
 public void Add(BarcodeDB barcode)
 {
     barcode.Created = DateTime.UtcNow;
     _wasteContext.Barcodes.Add(barcode);
     _wasteContext.SaveChanges();
 }
        public async Task SeedAsync(IList <string> prodIds)
        {
            await CreateUsers();

            var user = AddFriendsToFirstUser();

            CreateProductsAndAddThemToTheUsers();

            CreateGroups();

            await _context.SaveChangesAsync().ConfigureAwait(false);

            async Task CreateUsers()
            {
                for (int i = 0; i < 10; i++)
                {
                    var userToCreate = new UserDB
                    {
                        Id             = i.ToString(),
                        UserName       = $"UserName{i}",
                        Email          = _faker.Internet.Email(),
                        EmailConfirmed = true,
                        Created        = DateTime.UtcNow.AddDays(-15)
                    };
                    _context.Users.Add(userToCreate);
                }
                await _context.SaveChangesAsync().ConfigureAwait(false);

                for (int i = 0; i < 10; i++)
                {
                    await _manager.AddPasswordAsync(i.ToString(), $"{i}{i}{i}{i}{i}{i}").ConfigureAwait(false);
                }
            }

            UserDB AddFriendsToFirstUser()
            {
                var user0 = _context.Users.Include(u => u.Friends).Include(u => u.ProductDescriptions.Select(d => d.Product)).First(u => u.Id == "0");
                var user1 = _context.Users.Include(u => u.Friends).Include(u => u.ProductDescriptions.Select(d => d.Product)).First(u => u.Id == "1");
                var user2 = _context.Users.Include(u => u.Friends).Include(u => u.ProductDescriptions.Select(d => d.Product)).First(u => u.Id == "2");
                var user3 = _context.Users.Include(u => u.Friends).Include(u => u.ProductDescriptions.Select(d => d.Product)).First(u => u.Id == "3");

                user0.Friends.Add(user1);
                user0.Friends.Add(user2);
                user0.Friends.Add(user3);

                user1.Friends.Add(user0);

                user0.Modified = DateTime.UtcNow.AddDays(-3);

                return(user0);
            }

            void CreateProductsAndAddThemToTheUsers()
            {
                for (int i = 0; i < 7; i++)
                {
                    var rating          = _random.Next(1, 6);
                    var prodDescription = new UserProductDescriptionDB
                    {
                        UserId      = "0",
                        ProductId   = prodIds[i],
                        Rating      = rating,
                        Description = productDescriptions[rating - 1],
                        Created     = DateTime.UtcNow.AddDays(-2)
                    };
                    _context.UserProductDescriptions.Add(prodDescription);

                    _context.SaveChanges();
                }

                for (int i = 1; i < 10; i++)
                {
                    foreach (var prodId in prodIds)
                    {
                        var rating          = _random.Next(1, 6);
                        var prodDescription = new UserProductDescriptionDB
                        {
                            UserId      = i.ToString(),
                            ProductId   = prodId,
                            Rating      = rating,
                            Description = productDescriptions[rating - 1],
                            Created     = DateTime.UtcNow.AddDays(-2)
                        };
                        _context.UserProductDescriptions.Add(prodDescription);
                        _context.SaveChanges();
                    }
                }
            }

            void CreateGroups()
            {
                for (int i = 0; i < 3; i++)
                {
                    var group = new GroupDB
                    {
                        Id           = Guid.NewGuid().ToString(),
                        Name         = $"{_faker.Lorem.Word()} group",
                        Information  = _faker.Lorem.Sentence(),
                        AdminId      = i.ToString(),
                        Created      = DateTime.UtcNow.AddDays(-4),
                        IsNotDeleted = true
                    };
                    _context.Groups.Add(group);

                    for (int j = 0; j < 10; j++)
                    {
                        var groupUser = new GroupUserDB
                        {
                            UserName            = $"UserName{i}",
                            UserId              = j.ToString(),
                            GroupId             = group.Id,
                            IsConfirmed         = true,
                            RightToCreateBoards = true,
                            Created             = DateTime.UtcNow.AddDays(-1)
                        };
                        _context.GroupUsers.Add(groupUser);
                    }

                    for (int j = 0; j < 2; j++)
                    {
                        var gbName     = $"Board about {_faker.Lorem.Word()}";
                        var groupBoard = new GroupBoardDB
                        {
                            Id           = Guid.NewGuid().ToString(),
                            GroupId      = group.Id,
                            CreatorId    = i.ToString(),
                            Name         = gbName,
                            Information  = _faker.Lorem.Sentence(),
                            Created      = DateTime.UtcNow.AddDays(-2),
                            IsNotDeleted = true
                        };
                        _context.GroupBoards.Add(groupBoard);

                        var groupProduct = new GroupProductDB
                        {
                            Id           = Guid.NewGuid().ToString(),
                            GroupBoardId = groupBoard.Id,
                            Name         = _faker.Commerce.ProductName(),
                            ProductId    = prodIds[_random.Next(0, 9)],
                            Information  = _faker.Lorem.Sentence()
                        };
                        _context.GroupProducts.Add(groupProduct);

                        for (int k = 0; k < 10; k++)
                        {
                            var groupComment = new GroupCommentDB
                            {
                                Id            = Guid.NewGuid().ToString(),
                                GroupBoardId  = groupBoard.Id,
                                CommentatorId = _random.Next(0, 10).ToString(),
                                Comment       = _faker.Lorem.Sentence()
                            };
                            _context.GroupComments.Add(groupComment);
                        }
                    }
                }
            }
        }