public async Task <IActionResult> GetDriverAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(id, out var guid)) { var error = ErrorResponsesService.NotFoundError(Target, $"Driver with id '{id}' not found."); return(NotFound(error)); } Model.Driver modelDriver; try { modelDriver = await repository.GetAsync(guid, cancellationToken).ConfigureAwait(false); } catch (DriverNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientDriver = Converter.DriverConverter.Convert(modelDriver); return(Ok(clientDriver)); }
public async Task <IActionResult> GetRouteAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(id, out var guid)) { var error = ErrorResponsesService.NotFoundError(Target, $"Route with id '{id}' not found."); return(BadRequest(error)); } Model.Route modelRoute; try { modelRoute = await routeRepository.GetAsync(guid, cancellationToken).ConfigureAwait(false); } catch (RouteNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var modelCheckpoints = await checkpointRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); var clientRoute = Converter.RouteConverter.Convert(modelRoute, modelCheckpoints); return(Ok(clientRoute)); }
public async Task <IActionResult> GetSecretAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(id, out var guid) || guid.Equals(Guid.Empty)) { var error = ErrorResponsesService.NotFoundError(Target, $"Secret with id '{id}' not found."); return(NotFound(error)); } Model.Secret modelSecret; try { modelSecret = await repository.GetAsync(guid, cancellationToken).ConfigureAwait(false); } catch (SecretNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientSecret = Converter.SecretConverter.Convert(modelSecret); return(Ok(clientSecret)); }
public async Task <IActionResult> PatchTagAsync([FromRoute] string id, [FromBody] Client.TagPatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(patchInfo)); return(BadRequest(error)); } var modelPatchInfo = Converter.TagPatchInfoConverter.Convert(id, patchInfo); Model.Tag modelTag; try { modelTag = await repository.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (TagNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientTag = Converter.TagConverter.Convert(modelTag); return(Ok(clientTag)); }
public async Task <IActionResult> GetWayBillAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(id, out var guid)) { var error = ErrorResponsesService.NotFoundError(Target, $"WayBill with id '{id}' not found."); return(NotFound(error)); } Model.WayBill modelWayBill; try { modelWayBill = await wayBillRepository.GetAsync(guid, cancellationToken).ConfigureAwait(false); } catch (WayBillNotFoundException) { var error = ErrorResponsesService.NotFoundError(Target, $"WayBill with id '{id}' not found."); return(NotFound(error)); } var driver = await driverRepository.GetAsync(modelWayBill.Driver, cancellationToken).ConfigureAwait(false); var clientDriver = ModelConverters.Drivers.DriverConverter.Convert(driver); var vehicle = await vehicleRepository.GetAsync(modelWayBill.Vehicle, cancellationToken).ConfigureAwait(false); var clientVehicle = ModelConverters.Vehicles.VehicleConverter.Convert(vehicle); var route = await routeRepository.GetAsync(modelWayBill.Route, cancellationToken).ConfigureAwait(false); var checkpoints = await checkpointRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); var clientRoute = ModelConverters.Routes.RouteConverter.Convert(route, checkpoints); var mechanicNote = await mechanicNoteRepository.GetAsync(modelWayBill.MechanicNote, cancellationToken) .ConfigureAwait(false); var medicNotesCount = route.Checkpoints.Count + 1; var medicNotes = new Models.MedicNotes.MedicNote[medicNotesCount]; for (var i = 0; i < medicNotesCount; i++) { medicNotes[i] = await medicNoteRepository.CreateAsync(cancellationToken).ConfigureAwait(false); } var clientMechanicNote = ModelConverters.MechanicNotes.MechanicNoteConverter.Convert(mechanicNote); var clientMedicNotes = medicNotes.Select(ModelConverters.MedicNotes.MedicNoteConverter.Convert); var clientWayBill = WayBillConverter.Convert(modelWayBill, clientDriver, clientVehicle, clientRoute, clientMechanicNote, clientMedicNotes); return(Ok(clientWayBill)); }
public async Task <IActionResult> GetAllRoles(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var roles = await Task.Run(() => roleManager.Roles, cancellationToken); if (roles == null) { var error = ErrorResponsesService.NotFoundError(TargetRole, "Roles not found."); return(NotFound(error)); } var clientRoles = roles.Select(item => Converter.RoleConverter.Convert(item)).ToImmutableList(); return(Ok(clientRoles)); }
public async Task <IActionResult> DeleteTagAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); try { await repository.RemoveAsync(id, cancellationToken).ConfigureAwait(false); } catch (TagNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } return(NoContent()); }
public async Task <IActionResult> GetTagAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); Model.Tag modelTag; try { modelTag = await repository.GetAsync(id, cancellationToken).ConfigureAwait(false); } catch (TagNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientTag = Converter.TagConverter.Convert(modelTag); return(Ok(clientTag)); }
public IActionResult DeleteImageAsync([FromRoute] string imageName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (imageName == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(imageName)); return(BadRequest(error)); } var fileInfo = new FileInfo($"{hostingEnvironment.WebRootPath}{ImageDirectory}{imageName}"); if (!fileInfo.Exists) { var error = ErrorResponsesService.NotFoundError(Target, "Image not found"); return(NotFound(error)); } fileInfo.Delete(); return(NoContent()); }
public async Task <ActionResult> DeleteUserAsync([FromRoute] string userName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!HasAccessToUser(userName)) { return(Forbid()); } var user = await userManager.FindByNameAsync(userName); if (user == null) { var error = ErrorResponsesService.NotFoundError(Target, $"User with name '{userName}' not found."); return(NotFound(error)); } await userManager.DeleteAsync(user); return(NoContent()); }
public async Task <IActionResult> GetUserAsync([FromRoute] string userName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!HasAccessToUser(userName)) { return(Forbid()); } var user = await userManager.FindByNameAsync(userName); if (user == null) { var error = ErrorResponsesService.NotFoundError(Target, $"User with name '{userName}' not found."); return(BadRequest(error)); } var clientUser = ModelConverters.Users.UserConverter.Convert(user); return(Ok(clientUser)); }
public async Task <IActionResult> DeleteDriverAsync([FromRoute] string id, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(id, out var guid)) { var error = ErrorResponsesService.NotFoundError(Target, $"Driver with id '{id}' not found."); return(NotFound(error)); } try { await repository.RemoveAsync(guid, cancellationToken).ConfigureAwait(false); } catch (DriverNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } return(NoContent()); }
public async Task <IActionResult> PatchRoleAsync([FromRoute] string userName, [FromBody] Client.RolePatchInfo clientRolePatchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (clientRolePatchInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(clientRolePatchInfo)); return(BadRequest(error)); } var user = await userManager.FindByNameAsync(userName); if (user == null) { var error = ErrorResponsesService.NotFoundError(TargetUser, $"User with name '{userName}' not found."); return(NotFound(error)); } var modelRolePatchInfo = Converter.RolePatchInfoConverter.Convert(userName, clientRolePatchInfo); var modelRole = await roleManager.FindByNameAsync(modelRolePatchInfo.UserRole.ToLower()); if (modelRole == null) { var error = ErrorResponsesService.NotFoundError(TargetRole, $"Role with name '{modelRolePatchInfo.UserRole}' not found."); return(NotFound(error)); } if (user.Roles.Contains(modelRolePatchInfo.UserRole.ToUpper())) { await userManager.RemoveFromRoleAsync(user, modelRolePatchInfo.UserRole); } else { await userManager.AddToRoleAsync(user, modelRolePatchInfo.UserRole); } return(Ok(user)); }
public async Task <IActionResult> PatchMedicNoteAsync([FromRoute] string id, [FromBody] Client.MedicNotePatchInfo 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, $"MedicNote with id '{id}' not found."); return(NotFound(error)); } var userName = HttpContext.User.Identity.Name; var user = await userManager.FindByNameAsync(userName); var modelPatchInfo = Converter.MedicNotePatchInfoConverter.Convert(guid, user.Name, patchInfo); Model.MedicNote modelMedicNote; try { modelMedicNote = await repository.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (MedicNoteNotFoundException ex) { var error = ErrorResponsesService.NotFoundError(Target, ex.Message); return(NotFound(error)); } var clientMedicNote = Converter.MedicNoteConverter.Convert(modelMedicNote); return(Ok(clientMedicNote)); }
public async Task <IActionResult> CreateWayBillAsync([FromBody] Client.WayBillCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(creationInfo)); return(BadRequest(error)); } Model.WayBillCreationInfo modelCreationInfo; try { modelCreationInfo = WayBillCreationInfoConverter.Convert(creationInfo); } catch (InvalidDataException ex) { var error = ErrorResponsesService.InvalidBodyData(nameof(creationInfo), ex.Message); return(BadRequest(error)); } if (!Guid.TryParse(creationInfo.RouteId, out var routeGuid)) { var error = ErrorResponsesService.NotFoundError("Route", $"Route with id '{creationInfo.RouteId}' not found."); return(BadRequest(error)); } Model.WayBill modelWayBill; var route = await routeRepository.GetAsync(routeGuid, cancellationToken).ConfigureAwait(false); var mechanicNote = await mechanicNoteRepository.CreateAsync(cancellationToken).ConfigureAwait(false); var medicNotesCount = route.Checkpoints.Count + 1; var medicNotes = new Models.MedicNotes.MedicNote[medicNotesCount]; for (var i = 0; i < medicNotesCount; i++) { medicNotes[i] = await medicNoteRepository.CreateAsync(cancellationToken).ConfigureAwait(false); } try { modelWayBill = await wayBillRepository.CreateAsync(modelCreationInfo, mechanicNote.Id, medicNotes.Select(item => item.Id), cancellationToken) .ConfigureAwait(false); } catch (WayBillDuplicationException ex) { var error = ErrorResponsesService.DuplicationError(Target, ex.Message); return(BadRequest(error)); } if (!Guid.TryParse(creationInfo.DriverId, out var driverGuid)) { var error = ErrorResponsesService.NotFoundError("Driver", $"Driver with id '{creationInfo.RouteId}' not found."); return(BadRequest(error)); } var driver = await driverRepository.GetAsync(driverGuid, cancellationToken).ConfigureAwait(false); var clientDriver = ModelConverters.Drivers.DriverConverter.Convert(driver); if (!Guid.TryParse(creationInfo.VehicleId, out var vehicleGuid)) { var error = ErrorResponsesService.NotFoundError("Vehicle", $"Vehicle with id '{creationInfo.RouteId}' not found."); return(BadRequest(error)); } var vehicle = await vehicleRepository.GetAsync(vehicleGuid, cancellationToken).ConfigureAwait(false); var clientVehicle = ModelConverters.Vehicles.VehicleConverter.Convert(vehicle); var checkpoints = await checkpointRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); var clientRoute = ModelConverters.Routes.RouteConverter.Convert(route, checkpoints); var clientMechanicNote = ModelConverters.MechanicNotes.MechanicNoteConverter.Convert(mechanicNote); var clientMedicNotes = medicNotes.Select(ModelConverters.MedicNotes.MedicNoteConverter.Convert); var clientWayBill = WayBillConverter.Convert(modelWayBill, clientDriver, clientVehicle, clientRoute, clientMechanicNote, clientMedicNotes); return(CreatedAtRoute("GetWayBillRoute", new { id = clientWayBill.Id }, clientWayBill)); }
public async Task <IActionResult> PatchUserAsync([FromRoute] string userName, [FromBody] Client.UserPatchInfo clientPatchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (clientPatchInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(clientPatchInfo)); return(BadRequest(error)); } if (!HasAccessToUser(userName)) { return(Forbid()); } var modelPatchInfo = ModelConverters.Users.UserPatchInfoConverter.Convert(userName, clientPatchInfo); var user = await userManager.FindByNameAsync(userName); if (user == null) { var error = ErrorResponsesService.NotFoundError(Target, $"User with name '{userName}' not found."); return(NotFound(error)); } var updated = false; if (modelPatchInfo.OldPassword != null && modelPatchInfo.Password != null) { var passwordHasher = HttpContext.RequestServices. GetService(typeof(IPasswordHasher <User>)) as IPasswordHasher <User>; //todo неработает проверка на правильность старого пароля // var oldPasswordHash = passwordHasher.HashPassword(user, modelPatchInfo.OldPassword); // // if (!oldPasswordHash.Equals(user.PasswordHash)) // { // var error = ErrorResponsesService.ValidationError(nameof(clientPatchInfo.OldPassword), // "Old password doesn't match with actual."); // return BadRequest(error); // } if (!modelPatchInfo.Password.Equals(modelPatchInfo.ConfirmPassword)) { var error = ErrorResponsesService.ValidationError(nameof(clientPatchInfo.ConfirmPassword), PasswordValidationMessage); return(BadRequest(error)); } var passwordValidator = HttpContext.RequestServices. GetService(typeof(IPasswordValidator <User>)) as IPasswordValidator <User>; var result = await passwordValidator.ValidateAsync(userManager, user, modelPatchInfo.Password); if (result.Succeeded) { user.PasswordHash = passwordHasher.HashPassword(user, modelPatchInfo.Password); updated = true; } } if (updated) { user.LastUpdateAt = DateTime.UtcNow; await userManager.UpdateAsync(user); } var clientUser = ModelConverters.Users.UserConverter.Convert(user); return(Ok(clientUser)); }