public async Task <IActionResult> AddPicture(string troubleId, IFormFile picture, CancellationToken cancellationToken) { var extension = Path.GetExtension(picture.FileName); var dir = GetDirName(_hostingEnvironment, troubleId); CreateDirectoryIfNotExists(dir); var path = $"{dir}/{GetAvailableIdForPicture(dir)}{extension}"; var response = await UploadPictureAsync(_hostingEnvironment, path, picture, cancellationToken); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var paths = GetImageUrlsForTrouble(_hostingEnvironment, troubleId); var troublePatchInfo = new TroublePatchInfo(TroubleConverterUtils.ConvertId(troubleId), null, null, paths, null, null, null, null, null); await _troubleRepository.PatchAsync(troublePatchInfo, cancellationToken).ConfigureAwait(false); return(Ok(new Picture(path))); } else { return(BadRequest(response)); } }
public async Task <IActionResult> PatchTroubleAsync([FromRoute] string id, [FromBody] Client.TroublePatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = Responses.BodyIsMissing(nameof(patchInfo)); return(BadRequest(error)); } var guid = Converter.TroubleConverterUtils.ConvertId(id); Model.Trouble trouble; try { trouble = await troubleRepository.GetAsync(guid, cancellationToken).ConfigureAwait(false); } catch (TroubleNotFoundException ex) { var error = Responses.NotFoundError(ex.Message, Target); return(BadRequest(error)); } var isAuthorized = trouble.Author == HttpContext.User.Identity.Name || HttpContext.User.IsInRole("admin"); if (!isAuthorized) { return(Forbid()); } if (!HttpContext.User.IsInRole("admin")) { patchInfo.Status = null; } Model.Trouble modelTrouble; try { var modelTags = await tagRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); var modelPatchInfo = Converter.TroublePatchInfoConverter.Convert(id, patchInfo, modelTags); modelTrouble = await troubleRepository.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { if (ex is TroubleNotFoundException) { var error = Responses.NotFoundError(ex.Message, Target); return(BadRequest(error)); } else { var error = Responses.InvalidData(ex.Message, Target); return(BadRequest(error)); } } var clientTrouble = Converter.TroubleConverter.Convert(modelTrouble); return(Ok(clientTrouble)); }