Example #1
0
        public int CreatePost(PostDto dto)
        {
            var post = new Post();

            post.MainPictureAdress = dto.MainPictureAdress;
            post.Title             = dto.Title;
            post.Date        = DateTime.Now;
            post.Description = dto.Description;
            post.Author      = _userContextService.User.Identity.Name;
            _context.Posts.Add(post);
            _context.SaveChanges();
            return(post.Id);
        }
Example #2
0
        public void RegisterUser(RegisterUserDto dto)
        {
            var newUser = new User()
            {
                Email     = dto.Email,
                FirstName = dto.FirstName,
                LastName  = dto.LastName,
                RoleId    = 1
            };
            var hashedPassword = _passwordHasher.HashPassword(newUser, dto.Password);

            newUser.PasswordHash = hashedPassword;
            _context.Users.Add(newUser);
            _context.SaveChanges();
        }
        public void AddAddress(AddressDto dto)
        {
            var userId  = _userContextService.GetUserId;
            var address = new Address()
            {
                Town        = dto.Town,
                Street      = dto.Street,
                HouseNumber = dto.HouseNumber,
                PostalCode  = dto.PostalCode
            };
            var user = _context.Users.Include(s => s.Address).FirstOrDefault(x => x.Id == userId);

            _context.Addresses.Add(address);
            user.Address.Add(address);
            _context.SaveChanges();
        }
Example #4
0
        public int Create(CreateOrderDto dto)
        {
            var products = _context.Products.Where(x => dto.ProductId.Keys.Contains(x.Id));

            if (products == null)
            {
                throw new EmptyListException("Lista produktów jest pusta");
            }

            foreach (var item in products)
            {
                if (item.Amount < dto.ProductId[item.Id])
                {
                    throw new BadRequestException($"Brak danej ilości produktu {item.Name} w magazynie");
                }
            }
            decimal suma = 0;

            var order = new Order()
            {
                UserId              = (int)_userContextService.GetUserId,
                AddressId           = dto.AddressId,
                Date                = DateTime.Now.ToShortDateString(),
                OrderStatusId       = 1,
                Products            = products.ToList(),
                OrderAmountProducts = new List <OrderAmountProducts>()
            };

            foreach (var item in products)
            {
                item.Amount -= dto.ProductId[item.Id];
                suma        += dto.ProductId[item.Id] * item.Price;
                order.OrderAmountProducts.Add(new OrderAmountProducts
                {
                    ProductId = item.Id,
                    Amount    = dto.ProductId[item.Id]
                });
            }


            order.TotalCost = suma;
            _context.Orders.Add(order);
            _context.SaveChanges();
            return(order.Id);
        }
Example #5
0
        void IFileService.AddForProduct(AddPictureDto dto)
        {
            var product = _context.Products.FirstOrDefault(x => x.Id == dto.ProductId);

            if (product == null)
            {
                throw new EmptyListException("Produkt nie istnieje");
            }

            product.PictureURL = $"http://visacomhotchocolate.cba.pl/images/{dto.PictureName}.jpg";
            _context.SaveChanges();
        }
 public void Seed()
 {
     if (_dbContext.Database.CanConnect())
     {
         if (!_dbContext.Roles.Any())
         {
             var roles = GetRoles();
             _dbContext.Roles.AddRange(roles);
             _dbContext.SaveChanges();
         }
     }
 }
Example #7
0
        public void Seed()
        {
            if (_dbContext.Database.CanConnect())
            {
                if (!_dbContext.Roles.Any())
                {
                    var roles = GetRoles();
                    foreach (var item in roles)
                    {
                        _dbContext.Add(item);
                        _dbContext.SaveChanges();
                    }
                }

                var user = _dbContext.Users.FirstOrDefault(x => x.Email == "*****@*****.**");
                if (user == null)
                {
                    var newAdmin = CreateAdmin();
                    _dbContext.Users.Add(newAdmin);
                    _dbContext.SaveChanges();
                }

                if (!_dbContext.Products.Any())
                {
                    var products = GetProducts();
                    _dbContext.AddRange(products);
                    _dbContext.SaveChanges();
                }

                if (!_dbContext.OrderStatuses.Any())
                {
                    var statuses = GetStatuses();
                    foreach (var item in statuses)
                    {
                        _dbContext.Add(item);
                        _dbContext.SaveChanges();
                    }
                }

                if (!_dbContext.Opinions.Any())
                {
                    _dbContext.Opinions.AddRange(GetOpinions());
                    _dbContext.SaveChanges();
                }

                if (!_dbContext.Orders.Any())
                {
                    _dbContext.Orders.AddRange(GetOrders());
                    _dbContext.SaveChanges();
                }

                if (!_dbContext.Posts.Any())
                {
                }
            }
        }