public async Task SetFields(int projectId, int characterId, Dictionary <int, string?> requestFieldValues) { var character = await LoadProjectSubEntityAsync <Character>(projectId, characterId); _ = character.RequestMasterAccess(CurrentUserId, acl => acl.CanEditRoles); _ = character.EnsureProjectActive(); var changedFields = FieldSaveHelper.SaveCharacterFields(CurrentUserId, character, requestFieldValues, FieldDefaultValueGenerator); MarkChanged(character); FieldsChangedEmail?email = null; if (changedFields.Any()) { var user = await UserRepository.GetById(CurrentUserId); email = EmailHelpers.CreateFieldsEmail( character, s => s.FieldChange, user, changedFields); } await UnitOfWork.SaveChangesAsync(); if (email != null) { await EmailService.Email(email); } }
//TODO: move character operations to a separate service. public async Task EditCharacter(int currentUserId, int characterId, int projectId, string name, bool isPublic, IReadOnlyCollection <int> parentCharacterGroupIds, bool isAcceptingClaims, string contents, bool hidePlayerForCharacter, IDictionary <int, string> characterFields, bool isHot) { var character = await LoadProjectSubEntityAsync <Character>(projectId, characterId); character.RequestMasterAccess(currentUserId, acl => acl.CanEditRoles); character.EnsureProjectActive(); var changedAttributes = new Dictionary <string, PreviousAndNewValue>(); changedAttributes.Add("Имя персонажа", new PreviousAndNewValue(name, character.CharacterName.Trim())); character.CharacterName = name.Trim(); character.IsAcceptingClaims = isAcceptingClaims; character.IsPublic = isPublic; var newDescription = new MarkdownString(contents); changedAttributes.Add("Описание персонажа", new PreviousAndNewValue(newDescription, character.Description)); character.Description = newDescription; character.HidePlayerForCharacter = hidePlayerForCharacter; character.IsHot = isHot; character.IsActive = true; character.ParentCharacterGroupIds = await ValidateCharacterGroupList(projectId, Required(parentCharacterGroupIds), ensureNotSpecial : true); var changedFields = FieldSaveHelper.SaveCharacterFields(currentUserId, character, characterFields, FieldDefaultValueGenerator); MarkChanged(character); MarkTreeModified(character.Project); //TODO: Can be smarter FieldsChangedEmail email = null; changedAttributes = changedAttributes .Where(attr => attr.Value.DisplayString != attr.Value.PreviousDisplayString) .ToDictionary(x => x.Key, x => x.Value); if (changedFields.Any() || changedAttributes.Any()) { var user = await UserRepository.GetById(currentUserId); email = EmailHelpers.CreateFieldsEmail( character, s => s.FieldChange, user, changedFields, changedAttributes); } await UnitOfWork.SaveChangesAsync(); if (email != null) { await EmailService.Email(email); } }
public async Task SaveFieldsFromClaim(int projectId, int characterId, IReadOnlyDictionary<int, string?> newFieldValue) { //TODO: Prevent lazy load here - use repository var claim = await LoadProjectSubEntityAsync<Claim>(projectId, characterId); var updatedFields = FieldSaveHelper.SaveCharacterFields(CurrentUserId, claim, newFieldValue, FieldDefaultValueGenerator); if (updatedFields.Any(f => f.Field.FieldBoundTo == FieldBoundTo.Character) && claim.Character != null) { MarkChanged(claim.Character); } var user = await GetCurrentUser(); var email = EmailHelpers.CreateFieldsEmail(claim, s => s.FieldChange, user, updatedFields); await UnitOfWork.SaveChangesAsync(); await EmailService.Email(email); }
public async Task <AccommodationRequest> SetAccommodationType(int projectId, int claimId, int roomTypeId) { //todo set first state to Unanswered var claim = await ClaimsRepository.GetClaim(projectId, claimId).ConfigureAwait(false); claim = claim.RequestAccess(CurrentUserId, acl => acl.CanSetPlayersAccommodations, claim?.ClaimStatus == Claim.Status.Approved ? ExtraAccessReason.PlayerOrResponsible : ExtraAccessReason.None); // Player cannot change accommodation type if already checked in if (claim.AccommodationRequest?.AccommodationTypeId == roomTypeId) { return(claim.AccommodationRequest); } var newType = await UnitOfWork.GetDbSet <ProjectAccommodationType>().FindAsync(roomTypeId) .ConfigureAwait(false); if (newType == null) { throw new JoinRpgEntityNotFoundException(roomTypeId, nameof(ProjectAccommodationType)); } var email = EmailHelpers.CreateFieldsEmail(claim, s => s.AccommodationChange, await GetCurrentUser(), new FieldWithPreviousAndNewValue[] { }, new Dictionary <string, PreviousAndNewValue>() { { "Тип поселения", //TODO[Localize] new PreviousAndNewValue(newType.Name, claim.AccommodationRequest?.AccommodationType.Name) }, }); var leaveEmail = await ConsiderLeavingRoom(claim); var accommodationRequest = new AccommodationRequest { ProjectId = projectId, Subjects = new List <Claim> { claim }, AccommodationTypeId = roomTypeId, IsAccepted = AccommodationRequest.InviteState.Accepted, }; UnitOfWork .GetDbSet <AccommodationRequest>() .Add(accommodationRequest); await UnitOfWork.SaveChangesAsync().ConfigureAwait(false); await EmailService.Email(email); if (leaveEmail != null) { await EmailService.Email(leaveEmail); } return(accommodationRequest); }