Пример #1
0
        public async Task <IActionResult> DeletePinFromBoard(
            DeletePinFromBoardDto model
            )
        {
            try
            {
                var responsePayload = await _boardPinService.DeletePinFromBoardAsync(model);

                return(Ok(responsePayload));
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }
            catch (Exception ex)
            {
                return(BadRequest(new { ex.Message }));
            }
        }
        public async Task <BoardReturnDto> DeletePinFromBoardAsync(DeletePinFromBoardDto model)
        {
            var userId = long.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var boardPinRelation =
                await(await _boardPinService.GetAllAsync(d => d.Pin.Id == model.PinId && d.Board.Id == model.BoardId,
                                                         x => x.Board))
                .FirstOrDefaultAsync();

            if (boardPinRelation.CreatedBy != userId)
            {
                throw new UnauthorizedAccessException();
            }


            var isLast =
                (await _boardPinService.GetAllAsync(d => d.Pin.Id == model.PinId)).Count() == 1;

            if (isLast)
            {
                var pin =
                    await _pinService.GetByIdAsync(model.PinId);

                await _pinService.RemoveAsync(pin);
            }


            if (boardPinRelation == null)
            {
                throw new ObjectNotFoundException("Relation not found.");
            }

            if (boardPinRelation.CreatedBy != userId)
            {
                throw new UnauthorizedAccessException();
            }

            await _boardPinService.RemoveAsync(boardPinRelation);

            return(boardPinRelation.Board.ToBoardReturnDto(true, isLast));
        }