Beispiel #1
0
        protected override async Task Handle(AddUserCommand request, CancellationToken cancellationToken)
        {
            var role = char.ToUpper(request.UserRole[0]) + request.UserRole.Substring(1).ToLower();

            var user = new Entities.User
            {
                Email       = request.Email,
                Password    = request.Password,
                DisplayName = request.DisplayName,
                UserRole    = (UserRole)Enum.Parse(typeof(UserRole), role)
            };

            _allMarktContext.Users.Add(user);
            await _allMarktContext.SaveChangesAsync(cancellationToken);

            switch (user.UserRole)
            {
            case UserRole.Admin:
            {
                var admin = new Admin {
                    User = user
                };
                _allMarktContext.Admins.Add(admin);
                break;
            }

            case UserRole.Moderator:
            {
                var moderator = new Moderator {
                    User = user
                };
                _allMarktContext.Moderators.Add(moderator);
                break;
            }

            case UserRole.Shop:
            {
                var shop = new Shop {
                    User = user
                };
                _allMarktContext.Shops.Add(shop);
                break;
            }

            case UserRole.Customer:
            {
                var customer = new Customer {
                    User = user
                };
                _allMarktContext.Customers.Add(customer);
                break;
            }

            default:
            {
                break;
            }
            }
            await _allMarktContext.SaveChangesAsync(cancellationToken);
        }
        protected override async Task Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _allMarktContext
                       .Users
                       .Include(u => u.ReceivedMessages)
                       .Include(u => u.SentMessages)
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            foreach (var message in user.ReceivedMessages)
            {
                message.Receiver = null;
            }

            foreach (var message in user.SentMessages)
            {
                message.Sender = null;
            }
            await _allMarktContext.SaveChangesAsync(cancellationToken);

            if (user != null)
            {
                _allMarktContext.Remove(user);

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #3
0
        protected override async Task Handle(EditPrivateMessageCommand request, CancellationToken cancellationToken)
        {
            var privateMessage = await _allMarktContext
                                 .PrivateMessages
                                 .FirstOrDefaultAsync(PM => PM.Id == request.Id);

            var sender = await _allMarktContext
                         .Users
                         .FirstOrDefaultAsync(user => user.Id == request.SenderId);

            var receiver = await _allMarktContext
                           .Users
                           .FirstOrDefaultAsync(user => user.Id == request.ReceiverId);

            if (receiver != null && sender != null)
            {
                privateMessage.Title     = request.Title;
                privateMessage.Text      = request.Text;
                privateMessage.DateRead  = request.DateRead;
                privateMessage.DateSent  = request.DateSent;
                privateMessage.Sender    = sender;
                privateMessage.Receiver  = receiver;
                privateMessage.DeletedBy = request.DeletedBy;

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #4
0
        protected override async Task Handle(AddOrderCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderItems != null)
            {
                Entities.Order          order         = new Entities.Order();
                ICollection <OrderItem> orderItemList = GetOrderItems(request.OrderItems, order);

                Shop     seller = _allMarktContext.Shops.Find(request.ShopId);
                Customer buyer  = _allMarktContext.Customers.Find(request.CustomerId);

                if (seller != null && buyer != null)
                {
                    order.Seller = seller;
                    order.Buyer  = buyer;
                    order.DeliveryPhoneNumber = request.DeliveryPhoneNumber;
                    order.DeliveryAddress     = request.DeliveryAddress;
                    order.TotalPrice          = request.TotalPrice;
                    order.TimeOfOrder         = DateTime.Now;
                    order.AdditionalNotes     = request.AdditionalNotes;
                    order.OrderStatus         = Status.Registered;
                    order.AWB        = request.AWB;
                    order.OrderItems = orderItemList;

                    _allMarktContext.Orders.Add(order);
                    _allMarktContext.OrderItems.AddRange(orderItemList);
                    await _allMarktContext.SaveChangesAsync(cancellationToken);
                }
            }
        }
Beispiel #5
0
        protected override async Task Handle(EditOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _allMarktContext
                        .Orders.Include(o => o.OrderItems)
                        .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (order != null)
            {
                order.DeliveryAddress     = request.DeliveryAddress;
                order.DeliveryPhoneNumber = request.DeliveryPhoneNumber;
                order.AdditionalNotes     = request.AdditionalNotes;
                order.AWB         = request.AWB;
                order.TotalPrice  = request.TotalPrice;
                order.OrderStatus = request.OrderStatus;

                if (request.OrderItems != null)
                {
                    var orderItemList = GetOrderItems(request.OrderItems, order);
                    _allMarktContext.OrderItems.RemoveRange(order.OrderItems.Except(orderItemList));
                    _allMarktContext.OrderItems.AddRange(orderItemList.Except(order.OrderItems));
                }

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
        protected override async Task Handle(DeleteProductCategoryCommand request, CancellationToken cancellationToken)
        {
            var productCategory = await _allMarktContext
                                  .ProductCategories
                                  .Include(pc => pc.Products)
                                  .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            foreach (var product in productCategory.Products)
            {
                _allMarktContext.Products.Remove(product);
            }
            await _allMarktContext.SaveChangesAsync(cancellationToken);

            if (productCategory != null)
            {
                _allMarktContext.ProductCategories.Remove(productCategory);
                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
        protected override async Task Handle(DeleteShopCategoryLinkCommand request, CancellationToken cancellationToken)
        {
            var shopCategory = await _allMarktContext
                               .ShopCategories
                               .Where(sc => sc.ShopId == request.ShopId)
                               .Where(sc => sc.CategoryId == request.CategoryId)
                               .FirstOrDefaultAsync();

            _allMarktContext.ShopCategories.Remove(shopCategory);
            await _allMarktContext.SaveChangesAsync();
        }
Beispiel #8
0
        protected override async Task Handle(DeleteOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _allmarktContext.Orders.Include(o => o.OrderItems).FirstOrDefaultAsync(
                x => x.Id == request.Id, cancellationToken);

            if (order != null)
            {
                _allmarktContext.Orders.Remove(order);
                await _allmarktContext.SaveChangesAsync(cancellationToken);
            }
        }
        protected override async Task Handle(AddShopCategoryLinkCommand request, CancellationToken cancellationToken)
        {
            _allMarktContext.ShopCategories.Add(
                new Entities.ShopCategory
            {
                ShopId     = request.ShopId,
                CategoryId = request.CategoryId
            }
                );

            await _allMarktContext.SaveChangesAsync(cancellationToken);
        }
Beispiel #10
0
        protected override async Task Handle(EditProductCategoryCommand request, CancellationToken cancellationToken)
        {
            var productCategory = await _allMarktContext.ProductCategories.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (productCategory != null)
            {
                productCategory.Name        = request.Name;
                productCategory.Description = request.Description;

                await _allMarktContext.SaveChangesAsync();
            }
        }
Beispiel #11
0
        protected override async Task Handle(EditShopCommentCommand request, CancellationToken cancellationToken)
        {
            var shopComment = await _allMarktContext
                              .ShopComments
                              .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (shopComment != null)
            {
                shopComment.Text = request.Text;
                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #12
0
        protected override async Task Handle(AddCategoryCommand request, CancellationToken cancellationToken)
        {
            _allMarktContext.Categories.Add(
                new Entities.Category
            {
                Name        = request.Name,
                Description = request.Description
            }
                );

            await _allMarktContext.SaveChangesAsync(cancellationToken);
        }
Beispiel #13
0
        protected override async Task Handle(DeletePrivateMessageCommand request, CancellationToken cancellationToken)
        {
            var privateMessage = await _allMarktContext
                                 .PrivateMessages
                                 .FirstOrDefaultAsync(x => x.Id == request.Id);

            if (privateMessage != null)
            {
                _allMarktContext.PrivateMessages.Remove(privateMessage);

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
        protected override async Task Handle(DeleteShopCommentCommand request, CancellationToken cancellationToken)
        {
            var shopComment = await _allMarktContext
                              .ShopComments
                              .FindAsync(request.Id);

            if (shopComment != null)
            {
                _allMarktContext.ShopComments.Remove(shopComment);

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #15
0
        protected override async Task Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
        {
            var category = await _allMarktContext
                           .Categories
                           .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (category != null)
            {
                _allMarktContext.Categories.Remove(category);

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #16
0
        protected override async Task Handle(DisableUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _allMarktContext
                       .Users
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (user != null)
            {
                user.IsEnabled = false;

                await _allMarktContext.SaveChangesAsync();
            }
        }
        protected override async Task Handle(EditCustomerCommand request, CancellationToken cancellationToken)
        {
            var customer = await _allMarktContext
                           .Customers
                           .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (customer != null)
            {
                customer.Address     = request.Address;
                customer.PhoneNumber = request.PhoneNumber;

                await _allMarktContext.SaveChangesAsync();
            }
        }
Beispiel #18
0
        protected override async Task Handle(EditShopCommand request, CancellationToken cancellationToken)
        {
            var shop = await _allMarktContext
                       .Shops
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (shop != null)
            {
                shop.Address     = request.Address;
                shop.IBAN        = request.IBAN;
                shop.PhoneNumber = request.PhoneNumber;

                await _allMarktContext.SaveChangesAsync();
            }
        }
Beispiel #19
0
        protected override async Task Handle(AddProductCategoryCommand request, CancellationToken cancellationToken)
        {
            var shop = await _allMarktContext.Shops.FindAsync(request.ShopId);

            if (shop != null)
            {
                _allMarktContext.ProductCategories.Add(new Entities.ProductCategory
                {
                    Name        = request.Name,
                    Description = request.Description,
                    Shop        = shop
                });

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #20
0
        protected override async Task Handle(EditProductCommand request, CancellationToken cancellationToken)
        {
            var product = await _allMarktContext
                          .Products
                          .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (product != null)
            {
                product.Name        = request.Name;
                product.Description = request.Description;
                product.Price       = request.Price;
                product.ImageURI    = request.ImageURI;
                product.State       = request.State;

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #21
0
        protected override async Task Handle(AddPrivateMessageCommand request, CancellationToken cancellationToken)
        {
            Entities.User sender = await _allMarktContext.Users.FirstOrDefaultAsync(user => user.Id == request.SenderId);

            Entities.User receiver = await _allMarktContext.Users.FirstOrDefaultAsync(user => user.Id == request.ReceiverId);

            _allMarktContext.PrivateMessages.Add(
                new Entities.PrivateMessage
            {
                Title    = request.Title,
                Text     = request.Text,
                DateSent = request.DateSent,
                DateRead = request.DateRead,
                Sender   = sender,
                Receiver = receiver
            });

            await _allMarktContext.SaveChangesAsync(cancellationToken);
        }
        protected override async Task Handle(AddProductCommentCommand request, CancellationToken cancellationToken)
        {
            var user = await _allMarktContext.Users.FindAsync(request.AddedByUserId);

            var product = await _allMarktContext.Products.FindAsync(request.ProductId);

            if (user != null && product != null)
            {
                _allMarktContext.ProductComments.Add(
                    new ProductComment
                {
                    Rating    = request.Rating,
                    Text      = request.Text,
                    AddedBy   = user,
                    Product   = product,
                    DateAdded = DateTime.Now
                });

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #23
0
        protected override async Task Handle(AddProductCommand request, CancellationToken cancellationToken)
        {
            var productCategory = _allMarktContext.ProductCategories.FindAsync(request.ProductCategoryId);

            if (productCategory != null)
            {
                _allMarktContext.Products.Add(
                    new Entities.Product
                {
                    Name            = request.Name,
                    Description     = request.Description,
                    Price           = request.Price,
                    ImageURI        = request.ImageURI,
                    State           = request.State,
                    ProductCategory = await productCategory
                }
                    );

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #24
0
        protected override async Task Handle(AddShopCommentCommand request, CancellationToken cancellationToken)
        {
            var user = await _allMarktContext.Users.FindAsync(request.AddedByUserId);

            var shop = await _allMarktContext.Shops.FindAsync(request.ShopId);

            if (user != null && shop != null)
            {
                _allMarktContext.ShopComments.Add(
                    new ShopComment
                {
                    Rating    = request.Rating,
                    Text      = request.Text,
                    AddedBy   = user,
                    Shop      = shop,
                    DateAdded = DateTime.Now
                });

                await _allMarktContext.SaveChangesAsync(cancellationToken);
            }
        }
Beispiel #25
0
        protected override async Task Handle(EditUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _allMarktContext
                       .Users
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (user != null)
            {
                var curentUserRole = user.UserRole;
                var role           = char.ToUpper(request.UserRole[0]) + request.UserRole.Substring(1).ToLower();
                var editUserRole   = (UserRole)Enum.Parse(typeof(UserRole), role);

                var requestPasswordHash = Hash.ComputeSha256Hash(request.NewPassword);
                var requestOldPassword  = Hash.ComputeSha256Hash(request.OldPassword);

                if (requestOldPassword == user.Password && requestPasswordHash != user.Password)
                {
                    user.Password = requestPasswordHash;
                }

                user.Email       = request.Email;
                user.DisplayName = request.DisplayName;
                user.UserRole    = (UserRole)Enum.Parse(typeof(UserRole), request.UserRole);

                await _allMarktContext.SaveChangesAsync(cancellationToken);

                if (curentUserRole != editUserRole)
                {
                    switch (curentUserRole)
                    {
                    case UserRole.Admin:
                    {
                        var admin = await _allMarktContext.Admins.FirstOrDefaultAsync(x => x.UserId == request.Id, cancellationToken);

                        _allMarktContext.Admins.Remove(admin);
                        break;
                    }

                    case UserRole.Moderator:
                    {
                        var moderator = await _allMarktContext.Moderators.FirstOrDefaultAsync(x => x.UserId == request.Id, cancellationToken);

                        _allMarktContext.Moderators.Remove(moderator);
                        break;
                    }

                    case UserRole.Shop:
                    {
                        var shop = await _allMarktContext.Shops.FirstOrDefaultAsync(x => x.UserId == request.Id, cancellationToken);

                        _allMarktContext.Shops.Remove(shop);
                        break;
                    }

                    case UserRole.Customer:
                    {
                        var customer = await _allMarktContext.Customers.FirstOrDefaultAsync(x => x.UserId == request.Id, cancellationToken);

                        _allMarktContext.Customers.Remove(customer);
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    await _allMarktContext.SaveChangesAsync(cancellationToken);

                    switch (editUserRole)
                    {
                    case UserRole.Admin:
                    {
                        var admin = new Admin {
                            User = user
                        };
                        _allMarktContext.Admins.Add(admin);
                        break;
                    }

                    case UserRole.Moderator:
                    {
                        var moderator = new Moderator {
                            User = user
                        };
                        _allMarktContext.Moderators.Add(moderator);
                        break;
                    }

                    case UserRole.Shop:
                    {
                        var shop = new Shop {
                            User = user
                        };
                        _allMarktContext.Shops.Add(shop);
                        break;
                    }

                    case UserRole.Customer:
                    {
                        var customer = new Customer {
                            User = user
                        };
                        _allMarktContext.Customers.Add(customer);
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    await _allMarktContext.SaveChangesAsync(cancellationToken);
                }
            }
        }
Beispiel #26
0
        public async Task <int> SetTestDataProductCategoriesQueryAsync()
        {
            var user = new AllMarkt.Entities.User
            {
                Email       = "*****@*****.**",
                DisplayName = "tets"
            };

            _allMarktContextIM.Users.Add(user);
            await _allMarktContextIM.SaveChangesAsync();

            var shop1 = new AllMarkt.Entities.Shop
            {
                Address           = "dddd",
                User              = user,
                Comments          = null,
                CUI               = "ddd",
                IBAN              = "dffddfdfd",
                Orders            = null,
                PhoneNumber       = "0123654789",
                ProductCategories = null,
                ShopCategoryLink  = null,
                SocialCapital     = 3
            };

            var shop2 = new AllMarkt.Entities.Shop
            {
                Address           = "aaa",
                User              = user,
                Comments          = null,
                CUI               = "ddd",
                IBAN              = "aaaa",
                Orders            = null,
                PhoneNumber       = "0123654789",
                ProductCategories = null,
                ShopCategoryLink  = null,
                SocialCapital     = 2
            };

            _allMarktContextIM.Shops.Add(shop1);
            _allMarktContextIM.Shops.Add(shop2);
            await _allMarktContextIM.SaveChangesAsync();

            _allMarktContextIM.ProductCategories.Add(new AllMarkt.Entities.ProductCategory
            {
                Description = "desc1",
                Name        = "name1",
                Shop        = shop1
            });
            _allMarktContextIM.ProductCategories.Add(new AllMarkt.Entities.ProductCategory
            {
                Description = "desc2",
                Name        = "name2",
                Shop        = shop1
            });

            _allMarktContextIM.ProductCategories.Add(new AllMarkt.Entities.ProductCategory
            {
                Description = "desc3",
                Name        = "name3",
                Shop        = shop2
            });
            await _allMarktContextIM.SaveChangesAsync();

            return(shop1.Id);
        }