Esempio n. 1
0
        public async Task <IActionResult> LikePost(long id)
        {
            var userId = GetUserIdFromClaims(User);

            if (userId == null)
            {
                return(Unauthorized(new UnauthorizedErrorViewModel()));
            }

            var post = await _postService.GetByIdAsync(id);

            if (post == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Post), id)));
            }

            var like = await _likeService.FindAsync(likeQuery =>
                                                    likeQuery.PostId == id && likeQuery.UserId == userId);

            if (like == null)
            {
                like = new Like {
                    PostId = post.Id, UserId = userId.Value
                };
                _likeService.Add(like);
            }
            else
            {
                _likeService.Remove(like);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
Esempio n. 2
0
        public async Task <IActionResult> GetPost(long id)
        {
            var userId = GetUserIdFromClaims(User);

            var cacheKey = $"{nameof(Post)}{id}";

            var postJson = await _cachingService.GetAsync(cacheKey);

            if (!string.IsNullOrWhiteSpace(postJson))
            {
                var viewModel = Deserialize <PostViewModel>(postJson);
                return(Ok(viewModel));
            }

            var post = await _postService.GetPublicByIdAsync(id, userId ?? 0);

            if (post == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Post), id)));
            }

            post = _processingService.ProcessPost(post, userId);

            var postViewModel = Mapper.Map <PostViewModel>(post);
            await _cachingService.AddAsync(cacheKey, Serialize(postViewModel));

            return(Ok(postViewModel));
        }
Esempio n. 3
0
        public async Task <IActionResult> GetCharacter(long id)
        {
            var character = await _characterService.GetWithAllByIdAsync(id);

            if (character == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Character), id)));
            }

            var mappedCharacter = Mapper.Map <GetCharacterViewModel>(character);
            var contributors    = await _suggestedEditService.GetContributorsForEntity(id);

            contributors = contributors.Where(contributor =>
                                              mappedCharacter.Contributors.All(contributor2 => contributor2.User.Name != contributor)).ToList();

            mappedCharacter.Contributors.AddRange(contributors.Select(name =>
                                                                      new ContributorViewModel()
            {
                ContributorType = ContributorType.Contributor,
                User            = new UserViewModel()
                {
                    Name = name
                }
            }).ToList());
            return(Ok(mappedCharacter));
        }
Esempio n. 4
0
        public async Task <IActionResult> GetUser(long id)
        {
            var user = await _applicationUserService.GetByIdAsync(id);

            if (user == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(User), id)));
            }

            return(MappedOk <UserViewModel>(user));
        }
Esempio n. 5
0
        public async Task <IActionResult> GetStage(long id)
        {
            var stage = await _stageService.GetWithGame(id);

            if (stage == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Stage), id)));
            }

            return(MappedOk <StageViewModel>(stage));
        }
Esempio n. 6
0
        public async Task <IActionResult> GetStage(long gameId, long stageId)
        {
            var stageModel = await _stageService.FindAsync(stage => stage.GameId == gameId && stage.Id == stageId);

            if (stageModel == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Stage), stageId)));
            }

            return(Ok(stageModel));
        }
Esempio n. 7
0
        public async Task <IActionResult> GetAllCharacters(long gameId)
        {
            var characters = await _characterService.GetCharactersByGameAsync(gameId);

            if (characters == null || !characters.Any())
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Character), gameId)));
            }

            return(MappedOk <List <GetCharacterListViewModel> >(characters));
        }
Esempio n. 8
0
        public async Task <IActionResult> GetCharacter(long gameId, long characterId)
        {
            var characterModel = await _characterService
                                 .FindAsync(character => character.GameId == gameId && character.Id == characterId);

            if (characterModel == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Character), characterId)));
            }

            return(MappedOk <GetCharacterViewModel>(characterModel));
        }
Esempio n. 9
0
        public async Task <IActionResult> UpdatePost(long id, CreatePostViewModel viewModel)
        {
            var userId = GetUserIdFromClaims(User);

            if (userId == null)
            {
                return(Unauthorized(new UnauthorizedErrorViewModel()));
            }

            var post = await _postService.GetByIdAsync(id);

            if (post == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Post), id)));
            }

            if (post.AuthorId != userId)
            {
                return(Forbid());
            }

            post.Title       = viewModel.Title;
            post.Body        = viewModel.Markdown;
            post.HTMLContent = viewModel.HTML;
            post.GameId      = viewModel.GameId;
            post.IsPrivate   = viewModel.IsPrivate;
            post.Tags        = string.Join(',', viewModel.Tags);
            post.Description = viewModel.Description;
            post.Category    = (PostCategory)viewModel.Category;

            _postService.Update(post);
            await _context.SaveChangesAsync();

            await _cachingService.RemoveAsync($"{nameof(Post)}{post.Id}");

            return(Ok());
        }
Esempio n. 10
0
        public async Task <IActionResult> GetGame(long gameId)
        {
            var cacheKey = $"{nameof(Game)}{gameId}";

            var gameJson = await _cachingService.GetAsync(cacheKey);

            if (!string.IsNullOrWhiteSpace(gameJson))
            {
                var gameViewmodel = Deserialize <GameViewModel>(gameJson);
                return(Ok(gameViewmodel));
            }

            var game = await _gameService.GetGameByIdAsync(gameId);

            if (game == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Game), gameId)));
            }

            var gameViewModel = Mapper.Map <GameViewModel>(game);
            await _cachingService.AddAsync(cacheKey, Serialize(gameViewModel));

            return(Ok(gameViewModel));
        }
Esempio n. 11
0
        public async Task <IActionResult> UpdateCharacter(long id, UpdateCharacterViewModel characterViewModel)
        {
            var currentCharacter = await _characterService.GetWithAllByIdAsync(id, false);

            if (currentCharacter == null)
            {
                return(NotFound(NotFoundErrorViewModel.Create(nameof(Character), id)));
            }

            var userId = GetUserIdFromClaims(User);

            if (!userId.HasValue)
            {
                return(Unauthorized(new UnauthorizedErrorViewModel()));
            }


            var character = Mapper.Map <Character>(characterViewModel);

            _characterFacadeService.UpdateCharacter(currentCharacter, character, userId.Value);
            await _dbContext.SaveChangesAsync();

            return(Accepted());
        }