Esempio n. 1
0
        public async Task <Unit> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            var entity = new Domain.Models.Order
            {
                Name          = request.Name,
                Line1         = request.Line1,
                Line2         = request.Line2,
                Line3         = request.Line3,
                City          = request.City,
                State         = request.State,
                Zip           = request.Zip,
                Country       = request.Country,
                GiftWrap      = request.GiftWrap,
                AddedDateTime = dateTime.Now
            };

            context.Orders.Add(entity);
            await context.SaveChangesAsync(cancellationToken);

            var lines = request.Lines.Select(s =>
                                             new OrderLine {
                GameId = s.Game.Id, OrderId = entity.Id, Quantity = s.Quantity
            });

            context.OrderLines.AddRange(lines);

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Esempio n. 2
0
        public async Task <Unit> Handle(UpdateGameCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.Games
                         .FirstOrDefaultAsync(r => r.Id == request.Id, cancellationToken);

            if (entity is null)
            {
                throw new NotFoundException(nameof(Domain.Models.Game), request.Id);
            }

            entity.Description = request.Description;
            entity.Price       = request.Price;
            entity.Name        = request.Name;

            request.SelectedCategories = request.SelectedCategories ?? Enumerable.Empty <int>();
            var gameCategories = request.SelectedCategories.ToList().Select(
                u => new Domain.Models.GameCategory {
                GameId = entity.Id, CategoryId = u
            });
            var savedGameCategories = await context.GameCategories
                                      .Where(gc => gc.GameId == entity.Id).ToListAsync(cancellationToken: cancellationToken);

            var resCategories = gameCategories.Except(savedGameCategories);

            context.GameCategories.AddRange(resCategories);

            resCategories = savedGameCategories.Except(gameCategories);
            context.GameCategories.RemoveRange(resCategories);

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
            public async Task <int> Handle(CreateCommentCommand request, CancellationToken cancellationToken)
            {
                var entity = new Comment()
                {
                    GameId          = request.GameId,
                    Content         = request.Content,
                    ParentCommentId = request.ParentCommentId
                };

                _context.Comments.Add(entity);

                if (!_context.Games.Any(game => game.Id == entity.GameId))
                {
                    throw new ConflictDataException(nameof(GameId),
                                                    $"Entity 'Game' ({entity.GameId}) was not found.");
                }

                if (entity.ParentCommentId.HasValue)
                {
                    var parentComment = (_context.Comments.Any(c => c.Id == entity.ParentCommentId))
                        ? await _context.Comments.FindAsync(entity.ParentCommentId)
                        : throw new ConflictDataException(nameof(ParentCommentId),
                                                          $"Entity 'Comment' ({entity.ParentCommentId}) was not found.");

                    if (parentComment.GameId != entity.GameId)
                    {
                        throw new ConflictDataException(nameof(GameId),
                                                        $"Comment can't have different GameId from his parent. Expected GameId: {parentComment.GameId}");
                    }
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
            public async Task <int> Handle(UpdatePublisherCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Publishers.FindAsync(request.Id);

                entity.Name = request.Name;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 5
0
            public async Task <int> Handle(CreatePublisherCommand request, CancellationToken cancellationToken)
            {
                var entity = new Publisher();

                _context.Publishers.Add(entity);

                entity.Name = request.Name;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
        public async Task <Unit> Handle(DeleteGameCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.Games.FirstOrDefaultAsync(r => r.Id == request.Id, cancellationToken);

            if (entity is null)
            {
                throw new NotFoundException(nameof(Domain.Models.Game), request.Id);
            }
            context.Games.Remove(entity);
            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
        {
            var entity = new Domain.Models.Category
            {
                Name          = request.Name,
                AddedDateTime = dateTime.Now
            };

            context.Categories.Add(entity);
            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Esempio n. 8
0
            public async Task <int> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
            {
                var entity = new Category();

                _context.Categories.Add(entity);

                entity.Name             = request.Name;
                entity.ParentCategoryId = request.ParentCategoryId ?? entity.ParentCategoryId; // TODO: check if category exists

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 9
0
            public async Task <Unit> Handle(DeletePublisherCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Publishers.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(Publisher), request.Id);
                }

                _context.Publishers.Remove(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 10
0
            public async Task <int> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Categories.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(Category), request.Id);
                }

                entity.Name             = request.Name ?? entity.Name;
                entity.ParentCategoryId = request.ParentCategoryId ?? entity.ParentCategoryId;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 11
0
            public async Task <int> Handle(UpdateCommentCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Comments.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(Comment), request.Id);
                }

                entity.Content   = request.Content ?? entity.Content;
                entity.IsDeleted = request.IsDeleted ?? entity.IsDeleted;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
        public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.Categories
                         .FirstOrDefaultAsync(r => r.Id == request.Id, cancellationToken);

            if (entity is null)
            {
                throw new NotFoundException(nameof(Category), request.Id);
            }

            entity.Name = request.Name;

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
            public async Task <int> Handle(CreateGameCommand request, CancellationToken cancellationToken)
            {
                var entity = new Game();

                _context.Games.Add(entity);

                entity.Name        = request.Name;
                entity.Description = request.Description ?? entity.Description;
                entity.Price       = request.Price ?? entity.Price;
                entity.PublisherId = request.PublisherId ?? entity.PublisherId;
                entity.DeveloperId = request.DeveloperId ?? entity.DeveloperId;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 14
0
            public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Categories
                             .Include(e => e.ChildCategories)
                             .FirstOrDefaultAsync(e => e.Id == request.Id, cancellationToken);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(Category), request.Id);
                }

                DeleteCategoryChildren(entity);
                _context.Categories.Remove(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <int> Handle(UpdateGameCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Games.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(Game), request.Id);
                }

                entity.Name        = request.Name ?? entity.Name;
                entity.Description = request.Description ?? entity.Description;
                entity.Price       = request.Price ?? entity.Price;
                entity.IsDeleted   = request.IsDeleted ?? entity.IsDeleted;
                entity.PublisherId = request.PublisherId ?? entity.PublisherId;
                entity.DeveloperId = request.DeveloperId ?? entity.DeveloperId;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
        public async Task <Unit> Handle(CreateGameCommand request, CancellationToken cancellationToken)
        {
            var entity = new Domain.Models.Game
            {
                Name          = request.Name,
                Description   = request.Description,
                Price         = request.Price,
                AddedDateTime = dateTime.Now
            };

            context.Games.Add(entity);

            request.SelectedCategories = request.SelectedCategories ?? Enumerable.Empty <int>();
            var gameCategories = request.SelectedCategories.ToList().Select(
                u => new Domain.Models.GameCategory {
                GameId = entity.Id, CategoryId = u
            });

            context.GameCategories.AddRange(gameCategories);

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Esempio n. 17
0
        private async Task SeedCategoriesAsync(CancellationToken cancellationToken)
        {
            if (_context.Categories.Any())
            {
                return;
            }

            var categories = new[]
            {
                new Category {
                    Name = "Strategy"
                },
                new Category {
                    Name = "RPG"
                },
                new Category {
                    Name = "Sports"
                },
                new Category {
                    Name = "Races"
                }
                .AddCategoryChild(
                    new Category {
                    Name = "Rally"
                },
                    new Category {
                    Name = "Arcade"
                },
                    new Category {
                    Name = "Formula"
                },
                    new Category {
                    Name = "Off-road"
                }
                    ),
                new Category {
                    Name = "Action"
                }
                .AddCategoryChild(
                    new Category {
                    Name = "FPS"
                },
                    new Category {
                    Name = "TPS"
                },
                    new Category {
                    Name = "Misc."
                }
                    ),
                new Category {
                    Name = "Adventure"
                },
                new Category {
                    Name = "Puzzle & Skill"
                },
                new Category {
                    Name = "Other"
                },
            };

            _context.Categories.AddRange(categories);

            await _context.SaveChangesAsync(cancellationToken);
        }