public async Task <long> AddPinAsync(AddPinDto model) { var userId = long.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); var usr = await(await _personService.GetAllAsync(d => d.Id == userId)).FirstOrDefaultAsync(); var boardInDb = await _boardService.GetByIdAsync(model.BoardId); if (boardInDb == null) { throw new ObjectNotFoundException("Board not found."); } if (boardInDb.CreatedBy != userId) { throw new UnauthorizedAccessException("You have no permissions to edit this board."); } var pin = await _pinService.InsertAsync(model.ToPin(usr)); var relation = new BoardPin { CreatedBy = usr.Id, Pin = pin, Board = boardInDb }; await _boardPinService.InsertAsync(relation); return(pin.Id); }
public static Pin ToPin(this AddPinDto model, Person person) { var pin = new Pin { Name = model.Name, Description = model.Description, Img = model.Img, Link = model.Link, CreatedBy = person.Id }; return(pin); }
public async Task <IActionResult> AddPin( AddPinDto model ) { try { var responsePayload = await _pinService.AddPinAsync(model); return(Ok(responsePayload)); } catch (UnauthorizedAccessException) { return(Unauthorized()); } catch (Exception ex) { return(BadRequest(new { ex.Message })); } }