public async Task <IActionResult> PatchMechanicNoteAsync([FromRoute] string id, [FromBody] Client.MechanicNotePatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(patchInfo)); return(BadRequest(error)); } if (!Guid.TryParse(id, out var guid)) { var error = ErrorResponsesService.NotFoundError(Target, $"MechanicNote with id '{id}' not found."); return(NotFound(error)); } var userName = HttpContext.User.Identity.Name; var user = await userManager.FindByNameAsync(userName); var modelPatchInfo = Converter.MechanicNotePatchInfoConverter.Convert(guid, user.Name, patchInfo); Model.MechanicNote modelMechanicNote; try { modelMechanicNote = await repository.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (MechanicNoteNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientMechanicNote = Converter.MechanicNoteConverter.Convert(modelMechanicNote); return(Ok(clientMechanicNote)); }
public static Model.MechanicNotePatchInfo Convert(Guid id, string mechanicName, Client.MechanicNotePatchInfo clientPatchInfo) { if (clientPatchInfo == null) { throw new ArgumentNullException(nameof(clientPatchInfo)); } if (clientPatchInfo.Params != null && clientPatchInfo.Params.Length != Model.MechanicNote.ParamsCount) { throw new ArgumentException( $"{nameof(clientPatchInfo.Params.Length)} must be equals {Model.MechanicNote.ParamsCount}."); } Permission?permission = null; if (Enum.TryParse(clientPatchInfo.Permission, out Permission perm)) { permission = perm; } var modelPatchInfo = new Model.MechanicNotePatchInfo(id, clientPatchInfo.Params, mechanicName, permission); return(modelPatchInfo); }