public async Task <Diver> EditDiverAsync(int diverId, EditDiverModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var existing = await _diverRepository.GetAsync(diverId); if (existing == null) { throw new NotFoundException("Водолаз не найден."); } existing.LastName = model.LastName; existing.FirstName = model.FirstName; existing.MiddleName = model.MiddleName; existing.PhotoUrl = model.PhotoUrl; existing.BirthDate = model.BirthDate; existing.RescueStationId = model.RescueStationId; existing.MedicalExaminationDate = model.MedicalExaminationDate; existing.Address = model.Address; existing.Qualification = model.Qualification; existing.PersonalBookNumber = model.PersonalBookNumber; existing.PersonalBookIssueDate = model.PersonalBookIssueDate; existing.PersonalBookProtocolNumber = model.PersonalBookProtocolNumber; var updated = await _diverRepository.UpdateAsync(existing); var hours = await SetWorkingHours(updated.DiverId, model.WorkingTime); var diver = _mapper.Map <Diver>(updated); diver.WorkingTime = hours; return(diver); }
public async Task <UseCaseResult> Handle([NotNull] EditUserProfile request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } logger.LogInformation("Edit user [{diverId}]", request.UserId, request); var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { logger.LogWarning("Diver for editing not found. Id [{diverId}]", request.UserId); return(UseCaseResult.NotFound()); } var user = await currentUser.GetCurrentDiverAsync(); var isAdmin = await currentUser.GetIsAdminAsync(); if (diver.Id != user.Id && !isAdmin) { logger.LogError("Access denied: User [{editorId}] is not allowed to edit user [{userId}]!", user.Id, request.UserId); return(UseCaseResult.AccessDenied()); } diver.Edit( user.Id, request.Fullname, request.Firstname, request.Lastname, request.Education, request.Experience, request.Slogan, request.MobilePhone, request.WebsiteUrl, request.FacebookId, request.TwitterHandle, request.SkypeId); await diverRepository.UpdateAsync(diver); logger.LogError("User [{editorId}] successfully edited by user [{userId}]!", user.Id, request.UserId, request); return(UseCaseResult.Success()); }
public async Task <UseCaseResult> Handle([NotNull] EditAvatar request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } logger.LogInformation("Changing avatar for user [{id}]", request.UserId, request.Avatar.Filename, request.Avatar.ContentType); var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { logger.LogError("Diver with Id [{id}] not found!", request.UserId); return(UseCaseResult.NotFound()); } if (!await currentUser.GetIsDiverOrAdmin(request.UserId)) { logger.LogError("Current user [{currentUser}] not allowed to change avatar of user [{user}]", currentUser.Username, diver.Realname); return(UseCaseResult.AccessDenied()); } var newAvatarId = await avatarStore.StoreAvatarAsync( diver.Firstname, diver.AvatarId, GetAvatarFileExt(request), request.Avatar.Content); diver.ChangeAvatar(newAvatarId); await diverRepository.UpdateAsync(diver); logger.LogInformation("Avatar of user [{userId}] change by user [{currentUser}]", diver.Realname, currentUser.Username); return(UseCaseResult.Success()); }