Exemplo n.º 1
0
        public IActionResult GetById(Guid id)
        {
            var game           = Service.GetById(id, User.GetSubjectOrThrow());
            var transferObject = GameTransferObject.FromGame(game);

            return(Ok(transferObject));
        }
Exemplo n.º 2
0
        public IActionResult Update([FromBody] GameTransferObject transferObject)
        {
            var userName = User.GetSubjectOrThrow();
            var game     = Game.FromTransferObject(transferObject);

            game.UserName = userName;
            Service.UpdateGame(game, userName);
            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Add([FromBody] GameTransferObject transferObject)
        {
            var game = Game.FromTransferObject(transferObject);

            try
            {
                var newGame = Service.AddGame(game, User.GetSubjectOrThrow());
                await MailService.SendOnGameAdded(newGame);

                return(Ok(newGame.Id.ToString()));
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 4
0
        internal static Game FromTransferObject(GameTransferObject transferObject)
        {
            Game game = null;

            switch (transferObject.ModelState)
            {
            case ModelState.New:
                game = new Game()
                {
                    Id          = Guid.NewGuid(),
                    AgeRatingId = transferObject.AgeRatingId,
                    Description = transferObject.Description,
                    Name        = transferObject.Name
                };
                break;

            default:
                game = new Game
                {
                    Id          = String.IsNullOrEmpty(transferObject.Id) ? Guid.NewGuid() : Guid.Parse(transferObject.Id),
                    AgeRatingId = transferObject.AgeRatingId,
                    Description = transferObject.Description,
                    Name        = transferObject.Name
                };
                break;
            }

            foreach (var item in transferObject.Categories)
            {
                game.GameCategories.Add(new GameCategory
                {
                    CategoryId = Guid.Parse(item.Id),
                    Category   = Category.FromTransferObject(item)
                });
            }
            return(game);
        }
Exemplo n.º 5
0
        private List <ItemSyncResponse> SyncGames(SyncTransferObject transferObject,
                                                  List <ItemSyncResponse> syncedCategories, string userName)
        {
            var result = new List <ItemSyncResponse>();

            using (var transaction = GamesService.NewTransaction())
            {
                try
                {
                    transferObject.Games.ForEach(game =>
                    {
                        game.Categories = game.Categories
                                          .Select(c =>
                        {
                            if (long.TryParse(c.Id, out _))
                            {
                                var syncedCategory = syncedCategories
                                                     .FirstOrDefault(syncedCat => syncedCat.OfflineId.Equals(c.Id));
                                if (syncedCategory != null)
                                {
                                    c.Id = syncedCategory.Id.ToString();
                                }
                            }
                            return(c);
                        })
                                          .ToList();

                        switch (game.ModelState)
                        {
                        case Models.ModelState.New:
                            var newGame = GamesService.AddGame(Game.FromTransferObject(game), userName);
                            result.Add(new ItemSyncResponse
                            {
                                OfflineId   = game.Id,
                                Id          = newGame.Id,
                                State       = ItemState.Created,
                                Replacement = GameTransferObject.FromGame(newGame)
                            });
                            break;

                        case Models.ModelState.Modified:
                            var gameToUpdate = Game.FromTransferObject(game);
                            var updatedGame  = GamesService.UpdateGame(gameToUpdate, userName);
                            result.Add(new ItemSyncResponse
                            {
                                OfflineId   = game.Id,
                                Id          = gameToUpdate.Id,
                                State       = ItemState.Updated,
                                Replacement = GameTransferObject.FromGame(updatedGame)
                            });
                            break;

                        case Models.ModelState.Deleted:
                            var id = Guid.Parse(game.Id);
                            GamesService.DeleteGame(id);
                            result.Add(new ItemSyncResponse
                            {
                                Id        = id,
                                OfflineId = id.ToString(),
                                State     = ItemState.Deleted
                            });
                            break;
                        }
                    });
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
            return(result);
        }