public async Task <ActionResult <PlanView> > Add([FromBody] NewPlanInput newPlanInput) { try { var validationResult = _newPlanInputValidator.Validate(newPlanInput); if (validationResult.IsValid) { var result = await _planService.AddPlanAsync(newPlanInput); return(result); } return(BadRequest()); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public async Task <IActionResult> Post([FromForm] PlanRequest plan) { string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; string url = $"{_configuration["AppUrl"]}Images/DefaultPlan.jpg"; string fullPath = null; if (plan.CoverFile != null) { string extension = Path.GetExtension(plan.CoverFile.FileName); if (!allowedExtensions.Contains(extension)) { return(BadRequest(new OperationResponse <string> { IsSuccess = false, Message = "Image type not supported", })); } if (plan.CoverFile.Length > 500000) { return(BadRequest(new OperationResponse <string> { IsSuccess = false, Message = "Image size cannot be bigger than 5MB", })); } string newFileName = $"Images/{Guid.NewGuid()}{extension}"; fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", newFileName); url = $"{_configuration["AppUrl"]}{newFileName}"; } var addedPlan = await _planService.AddPlanAsync(plan.Title, plan.Description, url, userId); if (addedPlan != null) { if (fullPath != null) { using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) { await plan.CoverFile.CopyToAsync(fileStream); } } return(Ok(new OperationResponse <Plan> { IsSuccess = true, Message = $"{addedPlan.Title} has been added successfully", OperationDate = DateTime.UtcNow, Record = addedPlan, })); } return(BadRequest(new OperationResponse <string> { IsSuccess = false, Message = "Adding plan failed", })); }