public async Task <IActionResult> Create([FromBody] CreateRequestFormModel model) { var result = await this.requests.Create(model, this.currentUser.Id); if (!result.Succeeded) { return(this.BadRequest(result.Errors)); } return(this.Ok()); }
public async Task <Result> Create(CreateRequestFormModel model, string userId) { var createAccountRequestType = this.dbContext.Set <RequestType>().FirstOrDefault(rt => rt.Type == "Create Account"); if (createAccountRequestType == null) { return(Result.Failure(WebConstants.DefaultErrorMessage)); } if (model.RequestTypeId != createAccountRequestType.Id && !model.AccountId.HasValue) { return(Result.Failure(ServiceConstants.AccountDoesNotExist)); } var request = new ExchangeratRequest() { RequestTypeId = model.RequestTypeId, Status = Status.Pending, UserId = userId, AccountTypeId = model.AccountTypeId, IssuedAt = DateTime.UtcNow }; if (model.RequestTypeId != createAccountRequestType.Id) { var isAccountOwner = await this.exchangeAccounts.IsOwner(model.AccountId.Value, userId); if (!isAccountOwner) { return(Result.Failure(ServiceConstants.AccountDoesNotExist)); } request.AccountId = model.AccountId; } this.dbContext.Add(request); await this.dbContext.SaveChangesAsync(); return(Result.Success); }