Ejemplo n.º 1
0
        public async Task <IActionResult> PostGameSession([FromBody] CreateNewGSViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var id = await _gameSessionRepository.CreateNewGameSession(model, User);

            return(Ok(new { Id = id }));
        }
        public async Task <int> CreateNewGameSession(CreateNewGSViewModel model, ClaimsPrincipal User)
        {
            string userID = User.FindAll(ClaimTypes.NameIdentifier).FirstOrDefault().Value;

            var userAdmin   = _db.Users.GetUserById(userID);
            int boardGameID = model.BoardGameID;

            if (model.IsCustomGame)
            {
                string boardGameImage = "";
                Guid   boardGameGuid  = Guid.NewGuid();
                // add boardgame image to blobStorage
                if (!string.IsNullOrEmpty(model.CustomGameImage))
                {
                    byte[] imageBoardGameBytes = Convert.FromBase64String(model.CustomGameImage);
                    Stream stream = new MemoryStream(imageBoardGameBytes);
                    await _blobStorage.Save(stream, $"{boardGameGuid}.jpg");

                    boardGameImage = _blobStorage.Load($"{boardGameGuid}.jpg").Uri.AbsoluteUri;
                }
                else
                {
                    boardGameImage = "https://zenit.org/wp-content/uploads/2018/05/no-image-icon.png";
                }

                //Create new board game
                var newBoardGame = new BoardGame
                {
                    Name           = model.CustomGameName,
                    ImageBoardGame = boardGameImage,
                    IsVerified     = false,
                    GuidBoardGame  = boardGameGuid,
                    Created        = DateTime.Now,
                    AddedBy        = userID,
                };
                await _db.AddBoardGame(newBoardGame);

                _logger.LogInformation(LoggingEvents.CreateNewGameSession, "Add new BoardGame to Database is successfully");

                boardGameID = _db.BoardGames.Where(x => x.GuidBoardGame == newBoardGame.GuidBoardGame).Select(x => x.ID).FirstOrDefault();
            }

            var gameSession = new GameSession
            {
                City        = model.City,
                Address     = model.Address,
                Name        = model.Name,
                Description = model.Description,
                TimeStart   = model.TimeStart,
                TimeEnd     = model.TimeEnd,
                IsCanceled  = false,
                Slots       = model.Slots,
                SlotsFree   = model.Slots - 1,
                UserAdminID = userAdmin.Id,
                Players     = new List <GameSessionApplicationUser>()
                {
                    new GameSessionApplicationUser
                    {
                        ApplicationUser   = userAdmin,
                        ApplicationUserID = userAdmin.Id
                    }
                },
                Created = DateTime.Now,
            };

            int gameSessionId = await _db.AddGameSession(gameSession);

            _logger.LogInformation(LoggingEvents.CreateNewGameSession, "Create new GameSession is successfully");

            var gsBoardGame = new GameSessionBoardGame()
            {
                GameSessionID = gameSession.ID,
                BoardGameID   = boardGameID
            };
            await _db.AddBoardGameToGameSession(gsBoardGame);

            _logger.LogInformation(LoggingEvents.CreateNewGameSession, "Add board games to new GameSession is successfully");

            var userProfile = await _db.Users.GetUserProfile(userID);

            await _db.UpdateNumberGamesSessionCreated(userProfile);

            return(gameSessionId);
        }