public async Task <AddToDoResponse> AddToDoAsync(Todo todo) { try { // Create a new to var newTodo = await _appDbContext.AddAsync(todo); await _appDbContext.SaveChangesAsync(); _logger.LogInformation($"Todo added"); // Using our _linkGenerator object var resourceLocation = _linkgenerator.GetPathByAction( "GetById", "todo", new { id = newTodo.Entity.Id, UserId = todo.UserId }); var response = new AddToDoResponse( todo.Id.ToString(), resourceLocation, true, null, ResponseMessageTypes.ADD_TODO_SUCCESS ); return(response); } catch { var response = new AddToDoResponse( string.Empty, string.Empty, false, new[] { new Error("refresh__token_failure", "Invalid or bad refresh token") }.ToList(), ResponseMessageTypes.ADD_TODO_FAILURE ); return(response); } }
public AddToDoResponse AddToDoItem([FromBody] AddToDoTaskRequest toDoTaskRequest) { var response = new AddToDoResponse(); try { if (_userService.AuthenticateSession(Request.Headers["Authorization"].ToString()) == false) { response.AddError("The authorization credentails were invalid", ErrorCode.SESSION_INVALID); return(response); } ActiveUser user = _userService.GetUserInformationFromAuthHeader(Request.Headers["Authorization"].ToString()); if (user.HouseId == 0) { response.AddError("You must belong to a household to add To Do Tasks", ErrorCode.USER_NOT_IN_HOUSEHOLD); return(response); } response.Id = _toDoRepository.AddToDoTask(toDoTaskRequest, user.HouseId); response.Notifications = new List <string> { $"The task '{toDoTaskRequest.Title}' has been added" }; } catch (ErrorCodeException exception) { response.AddError($"An unexpected exception occured: {exception}", toDoTaskRequest, exception.Code); } catch (Exception exception) { response.AddError($"An unexpected exception occured: {exception}", toDoTaskRequest); } return(response); }