public async Task AddUser(AddUser model)
        {
            var transaction = _dbContext.Database.BeginTransaction();

            try
            {
                var passwordHash = _passwordManager.GetHash(model.Password);

                var entity = new Data_Access_Layer.User()
                {
                    UserName     = model.Username,
                    FirstName    = model.FirstName,
                    LastName     = model.LastName,
                    Email        = model.Email,
                    PasswordHash = passwordHash
                };

                await UserRepository.Add(entity);

                Save();

                if (model.Role != null)
                {
                    var role = await RoleRepository.Find(model.Role.Name);

                    if (role != null)
                    {
                        await UserRoleRepository.Add(
                            new UserRole
                        {
                            User = entity,
                            Role = role
                        });

                        Save();
                    }
                }

                if (model.Claims != null)
                {
                    var claims = model.Claims.Select(cl => new UserClaim
                    {
                        UserId     = entity.Id,
                        ClaimType  = cl.Type,
                        ClaimValue = cl.Value
                    });

                    await UserClaimRepository.AddRange(claims);
                }

                Save();

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
Example #2
0
        public async Task <UserGetFullApiModel> Login(string login, string password, UserContactTypeEnum contactType)
        {
            var contact = login.ToLower();
            var hash    = _passwordManager.GetHash(password);

            User user = await _repository.FirstOrDefaultAsync(x => x.Email == contact && x.Password == hash);

            if (user == null)
            {
                throw new ArgumentException($"Login or password is not valid");
            }

            var userModel = _dataMapper.Parse <User, UserGetFullApiModel>(user);

            return(userModel);
        }
        public async Task AddUser(AddUser model)
        {
            await RunTaskInTransaction(async() =>
            {
                var passwordHash = _passwordManager.GetHash(model.Password);

                var entity = new Data_Access_Layer.User()
                {
                    UserName     = model.Username,
                    FirstName    = model.FirstName,
                    LastName     = model.LastName,
                    Email        = model.Email,
                    PasswordHash = passwordHash
                };

                await UserRepository.Add(entity);

                if (model.Role != null)
                {
                    var role = await RoleRepository.Find(model.Role.Name);

                    if (role != null)
                    {
                        await UserRoleRepository.Add(
                            new UserRole
                        {
                            UserId = entity.Id,
                            RoleId = role.Id
                        });
                    }
                }

                if (model.Claims != null)
                {
                    var claims = model.Claims.Select(cl => new UserClaim
                    {
                        UserId     = entity.Id,
                        ClaimType  = cl.Type,
                        ClaimValue = cl.Value
                    });

                    await UserClaimRepository.AddRange(claims);
                }

                return(string.Empty);
            });
        }
Example #4
0
        public void Migrate()
        {
            DropTables();
            CreateTables();

            if (UserRepository.Any())
            {
                return;
            }

            var currentDate = DateTime.Now;

            var roleClaims = ClaimsRepository
                             .GetClaims();

            var role = new Role
            {
                Name = "Administrator"
            };

            RoleRepository.Add(role);

            var role1 = new Role
            {
                Name = "Pracownik"
            };

            RoleRepository.Add(role1);

            var role2 = new Role
            {
                Name = "Księgowy"
            };

            RoleRepository.Add(role2);

            var role3 = new Role
            {
                Name = "Kierownik"
            };

            RoleRepository.Add(role3);

            var adminRoleClaims = roleClaims
                                  .Select(cl => new RoleClaim
            {
                RoleId     = role.Id,
                ClaimType  = cl.Type,
                ClaimValue = cl.Value
            });

            var kierownikRoleClaims = roleClaims
                                      .Skip(4)
                                      .Select(cl => new RoleClaim
            {
                RoleId     = role3.Id,
                ClaimType  = cl.Type,
                ClaimValue = cl.Value
            });

            var ksiegowyRoleClaims = roleClaims
                                     .Skip(8)
                                     .Select(cl => new RoleClaim
            {
                RoleId     = role2.Id,
                ClaimType  = cl.Type,
                ClaimValue = cl.Value
            });

            var pracownikRoleClaims = roleClaims
                                      .Skip(12)
                                      .Select(cl => new RoleClaim
            {
                RoleId     = role1.Id,
                ClaimType  = cl.Type,
                ClaimValue = cl.Value
            });

            RoleClaimRepository.AddRange(adminRoleClaims);
            RoleClaimRepository.AddRange(kierownikRoleClaims);
            RoleClaimRepository.AddRange(ksiegowyRoleClaims);
            RoleClaimRepository.AddRange(pracownikRoleClaims);

            var user = new User
            {
                UserName     = "******",
                FirstName    = "Jan",
                LastName     = "Kocur",
                Email        = "*****@*****.**",
                PasswordHash = _passwordManager.GetHash("123")
            };

            UserRepository.Add(user);

            var user1 = new User
            {
                UserName     = "******",
                FirstName    = "Władysław",
                LastName     = "Kocioł",
                Email        = "*****@*****.**",
                PasswordHash = _passwordManager.GetHash("123")
            };

            UserRepository.Add(user1);

            var user2 = new User
            {
                UserName     = "******",
                FirstName    = "Michał",
                LastName     = "Zbożny",
                Email        = "*****@*****.**",
                PasswordHash = _passwordManager.GetHash("123")
            };

            UserRepository.Add(user2);

            var user3 = new User
            {
                UserName     = "******",
                FirstName    = "Heniek",
                LastName     = "Luwer",
                Email        = "*****@*****.**",
                PasswordHash = _passwordManager.GetHash("123")
            };

            UserRepository.Add(user3);

            var userRole = new UserRole
            {
                RoleId = role.Id,
                UserId = user.Id
            };

            var userRole1 = new UserRole
            {
                RoleId = role2.Id,
                UserId = user1.Id
            };

            var userRole2 = new UserRole
            {
                RoleId = role1.Id,
                UserId = user2.Id
            };

            var userRole3 = new UserRole
            {
                RoleId = role3.Id,
                UserId = user3.Id
            };

            UserRoleRepository.Add(userRole);
            UserRoleRepository.Add(userRole1);
            UserRoleRepository.Add(userRole2);
            UserRoleRepository.Add(userRole3);

            var city = new City
            {
                Name = "Olsztyn"
            };

            CityRepository.Add(city);

            var city2 = new City
            {
                Name = "Warszawa"
            };

            CityRepository.Add(city2);

            var city3 = new City
            {
                Name = "Barczewo"
            };

            CityRepository.Add(city3);

            var city4 = new City
            {
                Name = "Gdańsk"
            };

            CityRepository.Add(city4);

            var city5 = new City
            {
                Name = "Kraków"
            };

            CityRepository.Add(city5);

            var counterparty = new Counterparty
            {
                Name        = "Firma sprzedajaca buty",
                NIP         = "1122334455",
                PhoneNumber = "1122334455666",
                Street      = "Lubelska 432",
                PostalCode  = "11-341",
                CityId      = city.Id
            };

            CounterpartyRepository.Add(counterparty);

            var counterparty1 = new Counterparty
            {
                Name        = "Firma sprzedajaca ubrania",
                NIP         = "2126337425",
                PhoneNumber = "1122334455666",
                Street      = "Lubelska 432",
                PostalCode  = "11-341",
                CityId      = city.Id
            };

            CounterpartyRepository.Add(counterparty1);

            var location = new Location
            {
                Name = "Półka X"
            };

            LocationRepository.Add(location);

            var location1 = new Location
            {
                Name = "Półka Y"
            };

            LocationRepository.Add(location1);

            var location2 = new Location
            {
                Name = "Półka Z"
            };

            LocationRepository.Add(location2);

            var location3 = new Location
            {
                Name = "Półka K"
            };

            LocationRepository.Add(location3);

            var invoice = new Invoice
            {
                DocumentId     = string.Format("FAK/{0:yyyyMMddhhmmssfff}", currentDate),
                CounterpartyId = counterparty.Id,
                CityId         = city.Id,
                InvoiceType    = Common.InvoiceType.Purchase,
                PaymentMethod  = Common.PaymentMethod.Card,
                IssueDate      = currentDate,
                CompletionDate = currentDate
            };

            InvoiceRepository.Add(invoice);

            var entries = new List <Entry>()
            {
                new Entry
                {
                    Name      = "Buty Halwin Meain XL",
                    InvoiceId = invoice.Id,
                    Price     = 22.22m,
                    Count     = 1000,
                    VAT       = 0.01m
                }
            };

            EntryRepository.AddRange(entries);

            invoice.Total = entries[0].Count * entries[0].Price;
            invoice.VAT   = invoice.Total * entries[0].VAT;
            InvoiceRepository.Update(invoice);

            currentDate = DateTime.Now;

            var invoice1 = new Invoice
            {
                DocumentId     = string.Format("FAK/{0:yyyyMMddhhmmssfff}", currentDate),
                CounterpartyId = counterparty.Id,
                CityId         = city.Id,
                InvoiceType    = Common.InvoiceType.Sales,
                PaymentMethod  = Common.PaymentMethod.Card,
                IssueDate      = currentDate,
                CompletionDate = currentDate
            };

            InvoiceRepository.Add(invoice1);

            var entries1 = new List <Entry>()
            {
                new Entry
                {
                    Name      = "Buty Halwin Meain XL",
                    InvoiceId = invoice1.Id,
                    Price     = 22.22m,
                    Count     = 100,
                    VAT       = 0.01m
                }
            };

            EntryRepository.AddRange(entries1);
            invoice1.Total = entries1[0].Count * entries1[0].Price;
            invoice1.VAT   = invoice1.Total * entries1[0].VAT;
            InvoiceRepository.Update(invoice1);

            currentDate = DateTime.Now;

            var invoice2 = new Invoice
            {
                DocumentId     = string.Format("FAK/{0:yyyyMMddhhmmssfff}", currentDate),
                CounterpartyId = counterparty.Id,
                CityId         = city.Id,
                InvoiceType    = Common.InvoiceType.Purchase,
                PaymentMethod  = Common.PaymentMethod.Card,
                IssueDate      = currentDate,
                CompletionDate = currentDate
            };

            InvoiceRepository.Add(invoice2);

            var entries2 = new List <Entry>()
            {
                new Entry
                {
                    Name      = "Czapka Xiaming",
                    InvoiceId = invoice2.Id,
                    Price     = 70.22m,
                    Count     = 400,
                    VAT       = 0.10m
                }
            };

            EntryRepository.AddRange(entries2);
            invoice2.Total = entries2[0].Count * entries2[0].Price;
            invoice2.VAT   = invoice2.Total * entries2[0].VAT;
            InvoiceRepository.Update(invoice2);

            currentDate = DateTime.Now;

            var invoice3 = new Invoice
            {
                DocumentId     = string.Format("FAK/{0:yyyyMMddhhmmssfff}", currentDate),
                CounterpartyId = counterparty.Id,
                CityId         = city.Id,
                InvoiceType    = Common.InvoiceType.Purchase,
                PaymentMethod  = Common.PaymentMethod.Card,
                IssueDate      = currentDate,
                CompletionDate = currentDate
            };

            InvoiceRepository.Add(invoice3);

            var entries3 = new List <Entry>()
            {
                new Entry
                {
                    Name      = "Spodnie Jeans Amba M",
                    InvoiceId = invoice3.Id,
                    Price     = 51.74m,
                    Count     = 300,
                    VAT       = 0.12m
                }
            };

            EntryRepository.AddRange(entries3);
            invoice3.Total = entries3[0].Count * entries3[0].Price;
            invoice3.VAT   = invoice3.Total * entries3[0].VAT;
            InvoiceRepository.Update(invoice3);

            var attribute = new Attribute
            {
                Name = "Firma"
            };

            AttributeRepository.Add(attribute);

            var attribute1 = new Attribute
            {
                Name = "Rodzaj"
            };

            AttributeRepository.Add(attribute1);

            var product = new Product
            {
                Name    = "Buty Halwin Meain XL",
                Barcode = "Buty Halwin Meain XL",
            };

            var product1 = new Product
            {
                Name    = "Spodnie Jeans Amba M",
                Barcode = "Spodnie Jeans Amba M"
            };

            var product2 = new Product
            {
                Name    = "Koszula Meadow L",
                Barcode = "Koszula Meadow L"
            };

            var product3 = new Product
            {
                Name    = "Czapka Xiaming",
                Barcode = "Czapka Xiaming"
            };

            ProductRepository.Add(product);
            ProductRepository.Add(product1);
            ProductRepository.Add(product2);
            ProductRepository.Add(product3);

            var productAttributes = new List <ProductAttribute>()
            {
                new ProductAttribute
                {
                    ProductId   = product.Id,
                    AttributeId = attribute.Id,
                    Value       = "Xinmex"
                },
                new ProductAttribute
                {
                    ProductId   = product.Id,
                    AttributeId = attribute1.Id,
                    Value       = "Obuwie skórzane"
                },
                new ProductAttribute
                {
                    ProductId   = product1.Id,
                    AttributeId = attribute.Id,
                    Value       = "Abibas"
                },
                new ProductAttribute
                {
                    ProductId   = product1.Id,
                    AttributeId = attribute1.Id,
                    Value       = "Spodnie"
                },
                new ProductAttribute
                {
                    ProductId   = product2.Id,
                    AttributeId = attribute.Id,
                    Value       = "Pomba"
                },
                new ProductAttribute
                {
                    ProductId   = product2.Id,
                    AttributeId = attribute1.Id,
                    Value       = "Koszula bawelniana"
                }
            };

            ProductAttributeRepository.AddRange(productAttributes);

            var productDetails = new List <ProductDetail>()
            {
                new ProductDetail
                {
                    LocationId = location.Id,
                    ProductId  = product.Id,
                    Count      = 300
                },
                new ProductDetail
                {
                    LocationId = location1.Id,
                    ProductId  = product.Id,
                    Count      = 150
                },
                new ProductDetail
                {
                    LocationId = location.Id,
                    ProductId  = product1.Id,
                    Count      = 200
                },
                new ProductDetail
                {
                    LocationId = location1.Id,
                    ProductId  = product1.Id,
                    Count      = 150
                },
                new ProductDetail
                {
                    LocationId = location.Id,
                    ProductId  = product2.Id,
                    Count      = 600
                },
                new ProductDetail
                {
                    LocationId = location1.Id,
                    ProductId  = product2.Id,
                    Count      = 350
                },
                new ProductDetail
                {
                    LocationId = location.Id,
                    ProductId  = product3.Id,
                    Count      = 400
                },
                new ProductDetail
                {
                    LocationId = location1.Id,
                    ProductId  = product3.Id,
                    Count      = 250
                }
            };

            ProductDetailsRepository.AddRange(productDetails);

            var goodsDispatchedNote = new GoodsDispatchedNote
            {
                DocumentId   = string.Format("PZ/{0:yyyyMMddhhmmss}", currentDate),
                Remarks      = "Testowy opis...",
                InvoiceId    = invoice1.Id,
                IssueDate    = currentDate,
                DispatchDate = currentDate
            };

            GoodsDispatchedNoteRepository.Add(goodsDispatchedNote);

            var goodsReceivedNote = new GoodsReceivedNote
            {
                DocumentId  = string.Format("WZ/{0:yyyyMMddhhmmss}", currentDate),
                Remarks     = "Testowy opis...",
                InvoiceId   = invoice.Id,
                IssueDate   = currentDate,
                ReceiveDate = currentDate
            };

            GoodsReceivedNoteRepository.Add(goodsReceivedNote);
        }