Ejemplo n.º 1
0
        public void Execute(UpdateProductDto request)
        {
            if (_context.Products.Any(x => x.Id == request.Id))
            {
                var oneProduct = _context.Products.Find(request.Id);
                if (request.Name == oneProduct.Name)
                {
                    _validator.ValidateAndThrow(request);
                    oneProduct.Name        = request.Name;
                    oneProduct.Description = request.Description;
                    oneProduct.Price       = request.Price;
                    //oneProduct.Slika = UploadJedneSlike.UploadJednaSlika(request.Slika);
                    oneProduct.Quantity  += request.Quantity;
                    oneProduct.ModifiedAt = DateTime.Now;

                    _context.SaveChanges();
                }
                else
                {
                    _validatorName.ValidateAndThrow(request);
                    oneProduct.Name        = request.Name;
                    oneProduct.Description = request.Description;
                    oneProduct.Price       = request.Price;
                    //oneProduct.Slika = UploadJedneSlike.UploadJednaSlika(request.Slika);
                    oneProduct.Quantity  += request.Quantity;
                    oneProduct.ModifiedAt = DateTime.Now;

                    _context.SaveChanges();
                }
            }
            else
            {
                throw new EntityNotFoundException(request.Id, typeof(Product));
            }
        }
Ejemplo n.º 2
0
        public void Execute(RegisterDto request)
        {
            _validator.ValidateAndThrow(request);

            HashSet <UserUseCase> useCases = new HashSet <UserUseCase>();

            foreach (var uucId in request.UseCasesIds)
            {
                var userUsecase = new UserUseCase
                {
                    UserUseCaseId = uucId
                };
                useCases.Add(userUsecase);
            }

            _context.Users.Add(new Domain.User
            {
                FirstName    = request.FirstName,
                LastName     = request.LastName,
                UserName     = request.Username,
                Password     = request.Password,
                Email        = request.Email,
                UserUseCases = useCases
            });

            _context.SaveChanges();

            _sender.Send(new SendEmailDto
            {
                Content = "<h1>Successfully registered!</h1>",
                SendTo  = request.Email,
                Subject = "Registration"
            });
        }
Ejemplo n.º 3
0
 public void Execute(ChnageOrderStatusDto request)
 {
     if (_context.Orders.Any(x => x.Id == request.OrderId))
     {
         var order = _context.Orders
                     .Include(p => p.OrderInfo)
                     .ThenInclude(pd => pd.Product)
                     .FirstOrDefault(x => x.Id == request.OrderId);
         if (order == null)
         {
         }
         if (order.OrderStatus == OrderStatus.Recieved)
         {
         }
         if (order.OrderStatus == OrderStatus.Recieved || order.OrderStatus == OrderStatus.Delivered)
         {
             if (request.Status == OrderStatus.Canceled || request.Status == OrderStatus.Delivered)
             {
                 order.OrderStatus = request.Status;
                 if (request.Status == OrderStatus.Canceled)
                 {
                     foreach (var n in order.OrderInfo)
                     {
                         n.Product.Quantity += n.Quantity;
                     }
                 }
                 _context.SaveChanges();
             }
         }
     }
     else
     {
         throw new EntityNotFoundException(request.OrderId, typeof(Order));
     }
 }
Ejemplo n.º 4
0
        public void Execute(CreateOrderDto request)
        {
            _validator.ValidateAndThrow(request);
            int IdUser = actor.Id;
            var order  = new Order
            {
                UserId  = IdUser,
                Address = request.Address,
                Date    = request.OrderDate
            };

            foreach (var item in request.Items)
            {
                var product = _context.Products.Find(item.ProductId);
                product.Quantity -= item.Quantity;
                order.OrderInfo.Add(new OrderInfo
                {
                    Name      = product.Name,
                    Quantity  = item.Quantity,
                    Price     = product.Price,
                    ProductId = item.ProductId,
                });;
            }
            _context.Orders.Add(order);
            _context.SaveChanges();
        }
Ejemplo n.º 5
0
        public void Execute(BlogDto request)
        {
            _validator.ValidateAndThrow(request);

            ICollection <BlogCategory> categoryBlogs = new List <BlogCategory>();

            foreach (var catId in request.CategoryIds)
            {
                var blogCategory = new BlogCategory
                {
                    CategoryId = catId
                };
                categoryBlogs.Add(blogCategory);
            }

            var blog = new Blog
            {
                Name           = request.Name,
                Description    = request.Description,
                BlogCategories = categoryBlogs,
                UserId         = actor.Id
            };

            _context.Blogs.Add(blog);
            _context.SaveChanges();
        }
Ejemplo n.º 6
0
        public void Execute(BlogDto request, int id)
        {
            var blog = _context.Blogs.Find(id);

            if (blog == null)
            {
                throw new EntityNotFoundException(id, typeof(Blog));
            }

            _context.Database.ExecuteSqlRaw($"Delete from BlogCategory where BlogId = {id}");

            _validator.ValidateAndThrow(request);

            blog.Name        = request.Name;
            blog.Description = request.Description;
            blog.ModifiedAt  = DateTime.Now;
            var categoryIds = request.CategoryIds;

            ICollection <BlogCategory> blogCategories = new List <BlogCategory>();

            foreach (var categoryId in categoryIds)
            {
                var blogCategory = new BlogCategory
                {
                    BlogId     = id,
                    CategoryId = categoryId
                };
                blogCategories.Add(blogCategory);
            }
            blog.BlogCategories = blogCategories;
            _context.SaveChanges();
        }
        public void Execute(CategoryDto request)
        {
            _validator.ValidateAndThrow(request);

            var category = new Category
            {
                Name = request.Name
            };

            _context.Categories.Add(category);
            _context.SaveChanges();
        }
Ejemplo n.º 8
0
        public void Log(IUseCase useCase, IApplicationActor actor, object useCaseData)
        {
            _context.UseCaseLogs.Add(new Domain.UseCaseLog
            {
                Actor       = actor.Identity,
                Data        = JsonConvert.SerializeObject(useCaseData),
                Date        = DateTime.UtcNow,
                UseCaseName = useCase.Name
            });

            _context.SaveChanges();
        }
        public void Execute(CategoryDto request)
        {
            var jednaKategorija = _context.Categories.Find(request.Id);

            if (_context.Categories.Any(x => x.Id == request.Id))
            {
                if (request.Name == jednaKategorija.Name)
                {
                    var oneCategory = _context.Categories.Find(request.Id);
                    oneCategory.Name       = request.Name;
                    oneCategory.ModifiedAt = DateTime.Now;
                    _context.SaveChanges();
                }
                else
                {
                    if (_context.Categories.Any(x => x.Name == request.Name))
                    {
                        throw new ConflictException(request.Name, typeof(Category));
                    }
                    else
                    {
                        if (request.Name == null)
                        {
                            throw new EntityNotFoundException(request.Id, typeof(Category));
                        }
                        else
                        {
                            var oneCategory = _context.Categories.Find(request.Id);
                            oneCategory.Name       = request.Name;
                            oneCategory.ModifiedAt = DateTime.Now;
                            _context.SaveChanges();
                        }
                    }
                }
            }
            else
            {
                throw new EntityNotFoundException(request.Id, typeof(Category));
            }
        }
        public void Execute(int request)
        {
            var role = _context.Roles.Find(request);

            if (role == null)
            {
                throw new EntityNotFoundException(request, typeof(Role));
            }

            _context.Roles.Remove(role);

            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var product = _context.Products.Find(request);

            if (product == null)
            {
                throw new EntityNotFoundException(request, typeof(Product));
            }

            _context.Products.Remove(product);

            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var category = _context.Categories.Find(request);

            if (category == null)
            {
                throw new EntityNotFoundException(request, typeof(Category));
            }

            _context.Categories.Remove(category);

            _context.SaveChanges();
        }
        public void Execute(CategoryDto request)
        {
            _validator.ValidateAndThrow(request);
            var category = new Category
            {
                Name      = request.Name,
                CreatedAt = DateTime.Now,
                IsActive  = true
            };

            _context.Categories.Add(category);

            _context.SaveChanges();
        }
Ejemplo n.º 14
0
        public void Execute(CommentDto request, int id)
        {
            _validator.ValidateAndThrow(request);

            var comment = new Comment
            {
                Text   = request.Text,
                BlogId = id,
                UserId = _actor.Id
            };

            _context.Comments.Add(comment);
            _context.SaveChanges();
        }
        public void Execute(CategoryDto request, int id)
        {
            var category = _context.Categories.Find(id);

            if (category == null)
            {
                throw new EntityNotFoundException(id, typeof(Category));
            }

            _validator.ValidateAndThrow(request);

            category.Name       = request.Name;
            category.ModifiedAt = DateTime.Now;
            _context.SaveChanges();
        }
Ejemplo n.º 16
0
        public void Execute(CommentDto request, int id)
        {
            var comment = _context.Comments.Find(id);

            if (comment == null)
            {
                throw new EntityNotFoundException(id, typeof(Comment));
            }

            _validator.ValidateAndThrow(request);

            comment.Text       = request.Text;
            comment.ModifiedAt = DateTime.Now;
            _context.SaveChanges();
        }
Ejemplo n.º 17
0
        public void Execute(RoleDto request)
        {
            _validator.ValidateAndThrow(request);

            var role = new Role
            {
                Name      = request.Name,
                CreatedAt = DateTime.Now,
                IsActive  = true
            };

            _context.Roles.Add(role);

            _context.SaveChanges();
        }
Ejemplo n.º 18
0
        public void Execute(ProductDto request)
        {
            _validator.ValidateAndThrow(request);
            var product = new Product
            {
                Name        = request.Name,
                Price       = request.Price,
                Description = request.Description,
                Quantity    = request.Quantity,
                CategoryId  = request.CategoryId,
                CreatedAt   = DateTime.Now,
                IsActive    = true
            };

            _context.Products.Add(product);

            _context.SaveChanges();
        }
        public void Execute(int id)
        {
            var category = _context.Categories.Find(id);

            if (category == null)
            {
                throw new EntityNotFoundException(id, typeof(Category));
            }

            if (category.IsDeleted == true)
            {
                throw new AlreadyDeletedException(id, typeof(Category));
            }

            category.DeletedAt = DateTime.Now;
            category.IsActive  = false;
            category.IsDeleted = true;
            _context.SaveChanges();
        }
Ejemplo n.º 20
0
        public void Execute(UserUpdateDto request, int id)
        {
            var user = _context.Users.Find(id);

            if (user == null)
            {
                throw new EntityNotFoundException(id, typeof(User));
            }

            _validator.ValidateAndThrow(request);

            user.FirstName  = request.FirstName;
            user.LastName   = request.LastName;
            user.UserName   = request.Username;
            user.Email      = request.Email;
            user.ModifiedAt = DateTime.Now;

            _context.SaveChanges();
        }
Ejemplo n.º 21
0
        public void Execute(int request)
        {
            var blog = _context.Blogs.Find(request);

            if (blog == null)
            {
                throw new EntityNotFoundException(request, typeof(Blog));
            }

            if (blog.IsDeleted == true)
            {
                throw new AlreadyDeletedException(request, typeof(Blog));
            }

            blog.DeletedAt = DateTime.Now;
            blog.IsActive  = false;
            blog.IsDeleted = true;
            _context.SaveChanges();
        }
Ejemplo n.º 22
0
        public void Execute(int id)
        {
            var comment = _context.Comments.Find(id);

            if (comment == null)
            {
                throw new EntityNotFoundException(id, typeof(Comment));
            }

            if (comment.IsDeleted == true)
            {
                throw new AlreadyDeletedException(id, typeof(Comment));
            }

            comment.DeletedAt = DateTime.Now;
            comment.IsActive  = false;
            comment.IsDeleted = true;

            _context.SaveChanges();
        }
Ejemplo n.º 23
0
        public void Execute(ImageDto request)
        {
            if (_context.Products.Any(x => x.Id == request.ProductId))
            {
                validator.ValidateAndThrow(request);
                var newPath = UploadImageFunction.UploadImage(request.Path);

                var image = new Image
                {
                    Path      = newPath,
                    Alt       = request.Alt,
                    ProductId = request.ProductId
                };
                _context.Images.Add(image);

                _context.SaveChanges();
            }
            else
            {
                throw new EntityNotFoundException(request.ProductId, typeof(Product));
            }
        }
Ejemplo n.º 24
0
        public void Post([FromForm] ImageDto dto, int blogId)
        {
            var guid = Guid.NewGuid();
            var extension = Path.GetExtension(dto.Image.FileName);

            var newFileName = guid + extension;

            var path = Path.Combine("wwwroot", "images", newFileName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                dto.Image.CopyTo(fileStream);
            }

            var picture = new Picture
            {
                BlogId = blogId,
                Src = newFileName
            };

            _context.Pictures.Add(picture);
            _context.SaveChanges();
        }
Ejemplo n.º 25
0
        public void Execute(RegisterUserDto request)
        {
            _validator.ValidateAndThrow(request);

            _context.Users.Add(new User
            {
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Username  = request.Username,
                Password  = request.Password,
                Email     = request.Email,
                RoleId    = request.RoleId
            });

            _context.SaveChanges();

            _sender.Send(new SendEmailDto
            {
                Content = "<h1>Successfull registration!</h1>",
                SendTo  = request.Email,
                Subject = "Registration"
            });
        }
Ejemplo n.º 26
0
        public void Execute(int id)
        {
            var user = _context.Users.Find(id);

            if (user.Id == _actor.Id)
            {
                throw new DeleteYourselfException(id, typeof(User));
            }

            if (user == null)
            {
                throw new EntityNotFoundException(id, typeof(User));
            }

            if (user.IsDeleted == true)
            {
                throw new AlreadyDeletedException(id, typeof(User));
            }

            user.DeletedAt = DateTime.Now;
            user.IsActive  = false;
            user.IsDeleted = true;
            _context.SaveChanges();
        }