This commit is contained in:
2023-07-28 21:18:18 +03:00
parent 5ecefe5756
commit 90f0386bfe
53 changed files with 3067 additions and 267 deletions
+9 -10
View File
@@ -58,7 +58,6 @@ public class AccountController : BaseApiController
case GeneralExecStatus.success:
return _mapper.Map<AccountDetailedViewModel>(exec.Result);
//return exec.Result!.ToModel();
default:
throw new NotSupportedException(exec.Status.ToString());
@@ -92,11 +91,11 @@ public class AccountController : BaseApiController
switch (exec.Status)
{
case MotionAddResult.account_not_found:
case MotionAddStatus.account_not_found:
return ProblemBadRequest("Account not found.");
case MotionAddResult.failure:
case MotionAddStatus.failure:
return ProblemBadRequest("Adding motion failed.");
case MotionAddResult.success:
case MotionAddStatus.success:
return _mapper.Map<MotionViewModel[]>(exec.Result!);
default:
@@ -111,11 +110,11 @@ public class AccountController : BaseApiController
switch (exec.Status)
{
case MotionUpdateResult.not_found:
case MotionUpdateStatus.not_found:
return ProblemBadRequest("Motion not found.");
case MotionUpdateResult.failure:
case MotionUpdateStatus.failure:
return ProblemBadRequest("Updating motion failed.");
case MotionUpdateResult.success:
case MotionUpdateStatus.success:
return exec.Result!.ToModel();
default:
@@ -130,11 +129,11 @@ public class AccountController : BaseApiController
switch (exec.Status)
{
case MotionDeleteResult.not_found:
case MotionDeleteStatus.not_found:
return ProblemBadRequest("Motion not found.");
case MotionDeleteResult.failure:
case MotionDeleteStatus.failure:
return ProblemBadRequest("Deliting motion failed.");
case MotionDeleteResult.success:
case MotionDeleteStatus.success:
return exec.Result!.ToModel();
default:
@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Authorization;
using Models.Account;
using Services.Account;
using Services.Account.Domain;
using MyOffice.Web.Infrastructure.Attributes;
[Authorize]
[ApiController]
@@ -35,11 +36,11 @@ public class SettingsAccountController : BaseApiController
{
var list = _accountService.GetAllCategories(UserId);
return list.OrderBy(x => x.Name).Select(x => x.ToModel());
return _mapper.Map<AccountCategoryViewModel[]>(list).OrderBy(x => x.Name);
}
[HttpGet("~/api/settings/account-categories/{id}")]
public object AccountCategory(string id)
public object AccountCategory([AsGuid] string id)
{
var exec = _accountService.GetCategory(UserId, id.AsGuid());
@@ -50,7 +51,7 @@ public class SettingsAccountController : BaseApiController
return ProblemBadRequest("Account category not found.");
case GeneralExecStatus.success:
return exec.Result!.ToModel();
return _mapper.Map<AccountCategoryViewModel>(exec.Result!);
default:
throw new NotSupportedException(exec.Status.ToString());
@@ -76,7 +77,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpPut("~/api/settings/account-categories/{id}")]
public object AccountCategoriesEdit(string id, AccountCategoryViewModel request)
public object AccountCategoriesEdit([AsGuid] string id, AccountCategoryViewModel request)
{
var exec = _accountService.CategoryUpdate(UserId, id.AsGuid(), new AccountCategory { Name = request.Name! });
switch (exec.Status)
@@ -94,7 +95,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpDelete("~/api/settings/account-categories/{id}")]
public object AccountCategoriesDelete(string id)
public object AccountCategoriesDelete([AsGuid] string id)
{
var exec = _accountService.CategoryRemove(UserId, id.AsGuid());
switch (exec.Status)
@@ -115,7 +116,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpGet("~/api/settings/accounts")]
public List<AccountViewModel> AccountsGet(string? category)
public List<AccountViewModel> AccountsGet([AsGuid(true)] string? category)
{
var list = category.IsPresent()
? _accountService.GetByCategory(UserId, category!.AsGuid())
@@ -137,13 +138,13 @@ public class SettingsAccountController : BaseApiController
switch (exec.Status)
{
case AccountAddResult.category_not_found:
case AccountAddStatus.category_not_found:
return ProblemBadRequest("Category not found.");
case AccountAddResult.currency_not_found:
case AccountAddStatus.currency_not_found:
return ProblemBadRequest("Currency not found.");
case AccountAddResult.failure:
case AccountAddStatus.failure:
return ProblemBadRequest("Adding account failed.");
case AccountAddResult.success:
case AccountAddStatus.success:
return exec.Result!;
default:
@@ -153,7 +154,7 @@ public class SettingsAccountController : BaseApiController
[HttpPut("~/api/settings/accounts/{id}")]
public object AccountsAdd(string id, AccountEditRequestModel request)
public object AccountsAdd([AsGuid] string id, AccountEditRequestModel request)
{
var exec = _accountService.AccountUpdate(UserId, id.AsGuid(), new AccountEdit
{
@@ -166,15 +167,15 @@ public class SettingsAccountController : BaseApiController
switch (exec.Status)
{
case AccountEditResult.not_found:
case AccountEditStatus.not_found:
return ProblemBadRequest("Account not found.");
case AccountEditResult.category_not_found:
case AccountEditStatus.category_not_found:
return ProblemBadRequest("Category not found.");
case AccountEditResult.currency_not_found:
case AccountEditStatus.currency_not_found:
return ProblemBadRequest("Currency not found.");
case AccountEditResult.failure:
case AccountEditStatus.failure:
return ProblemBadRequest("Adding account failed.");
case AccountEditResult.success:
case AccountEditStatus.success:
return exec.Result!;
default:
@@ -183,7 +184,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpDelete("~/api/settings/accounts/{id}")]
public object AccountsDelete(string id)
public object AccountsDelete([AsGuid] string id)
{
var exec = _accountService.AccountDelete(UserId, id);
switch (exec.Status)
@@ -199,7 +200,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpDelete("~/api/settings/accounts/{id}/category/{categoryId}")]
public object AccountsCategoryRemove(string id, string categoryId)
public object AccountsCategoryRemove([AsGuid] string id, [AsGuid] string categoryId)
{
var exec = _accountService.AccountCategoryRemove(UserId, id.AsGuid(), categoryId.AsGuid());
@@ -216,4 +217,68 @@ public class SettingsAccountController : BaseApiController
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpPost("~/api/settings/accounts/{id}/access")]
public object AccountsAdd([AsGuid] string id, AccountAccessViewModel request)
{
var model = _mapper.Map<List<AccountAccessDto>>(request.Accesses);
var exec = _accountService.AccessUpdate(UserId, id.AsGuid(), model);
switch (exec.Status)
{
case GeneralExecStatus.not_found:
case GeneralExecStatus.failure:
return ProblemBadRequest("Account not found.");
case GeneralExecStatus.success:
break;
default:
throw new NotSupportedException(exec.Status.ToString());
}
if (!request.Email.IsPresent())
{
return exec.Result!;
}
var inviteExec = _accountService.AccessInvite(UserId, id.AsGuid(), request.Email!, request.AllowWrite);
switch (inviteExec.Status)
{
case AccessInviteStatus.account_not_found:
return ProblemBadRequest("Account not found.");
case AccessInviteStatus.access_exists:
return ProblemBadRequest("Access allowed.");
case AccessInviteStatus.invite_exists:
case AccessInviteStatus.success:
return exec.Result!;
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpDelete("~/api/settings/accounts/{id}/access/{userId}")]
public object AccountsAdd([AsGuid] string id, [AsGuid] string userId)
{
var exec = _accountService.AccessDelete(UserId, id.AsGuid(), userId.AsGuid());
switch (exec.Status)
{
case GeneralExecStatus.not_found:
case GeneralExecStatus.failure:
return ProblemBadRequest("Account not found.");
case GeneralExecStatus.success:
return exec.Result!;
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
}
@@ -0,0 +1,35 @@
namespace MyOffice.Web.Infrastructure.Attributes
{
using System.ComponentModel.DataAnnotations;
using Core.Extensions;
public class AsGuidAttribute: ValidationAttribute
{
private readonly bool _nullable;
public AsGuidAttribute(bool nullable = false)
{
_nullable = nullable;
}
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value == null && _nullable)
{
return ValidationResult.Success;
}
if (value == null || value.ToString().IsMissing())
{
return new ValidationResult("Id parameter is not valid");
}
if (!Guid.TryParse(value.ToString(), out _))
{
return new ValidationResult("Id parameter is not valid");
}
return ValidationResult.Success;
}
}
}
@@ -1,7 +1,6 @@
namespace MyOffice.Web.Models.Account
{
using Data.Models.Accounts;
using Services.Account.Domain;
using Newtonsoft.Json;
using User;
public class AccessRightsViewModel
@@ -10,19 +9,16 @@
public bool IsAllowRead { get; set; }
public bool IsAllowWrite { get; set; }
public bool IsAllowManage { get; set; }
}
public bool IsAllowDelete { get; set; }
public bool IsOwner { get; set; }
public static class AccessRightsViewModelExtensions
{
public static AccessRightsViewModel ToModel(this AccountAccessDto accountAccess)
{
return new AccessRightsViewModel
{
User = accountAccess.User!.ToModel(),
IsAllowManage = accountAccess.IsAllowManage,
IsAllowRead = accountAccess.IsAllowRead,
IsAllowWrite = accountAccess.IsAllowWrite,
};
}
#region DEBUG
[JsonIgnore]
public Guid UserId { get; set; }
[JsonIgnore]
public Guid OwnerId { get; set; }
#endregion DEBUG
}
}
+12 -60
View File
@@ -1,12 +1,7 @@
namespace MyOffice.Web.Models.Account
{
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using Core.Extensions;
using Data.Models.Accounts;
using Services.Account.Domain;
using Services.Identity;
using User;
public class AccountViewModel
{
@@ -20,71 +15,28 @@
public string CurrencyId { get; set; } = null!;
public string? CurrencyName { get; set; }
public bool AllowDelete { get; set; }
public bool AllowManage { get; set; }
public List<AccountCategoryViewModel>? Categories { get; set; }
[Required]
public string CategoryId { get; set; } = null!;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<AccessRightsViewModel>? AccessRights { get; set; }
}
public class ViewModelProfile : Profile
public class AccountAccessViewModel
{
public ViewModelProfile()
public class AccountAccessItemViewModel
{
CreateMap<Guid, string>().ConvertUsing(x => x.ToShort());
public string UserId { get; set; } = null!;
public bool AllowWrite { get; set; }
}
public string? Email { get; set; }
public bool AllowWrite { get; set; }
public List<AccountAccessItemViewModel> Accesses { get; set; } = null!;
}
public class AccountViewModelProfile : Profile
{
public AccountViewModelProfile(
IContextProvider contextProvider
)
{
CreateMap<AccountDetailedDto, AccountDetailedViewModel>()
.ForMember(x => x.Account, o => o.MapFrom(x => x.Account))
;
CreateMap<UserDto, UserViewModel>();
CreateMap<MyOffice.Data.Models.Users.User, UserViewModel>();
CreateMap<AccountDto, AccountViewModel>()
.ForMember(x => x.Type, o => o.MapFrom(x => x.AccessRights!.FirstOrDefault(a => a.UserId == contextProvider.UserId)!.Type.ToString()))
.ForMember(x => x.CurrencyId, o => o.MapFrom(x => x.CurrencyGlobalId))
.ForMember(x => x.CurrencyName, o => o.MapFrom(x => x.Currency!.Name))
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.HasMotions))
;
CreateMap<AccountAccountCategoryDto, AccountCategoryViewModel>()
.ForMember(x => x.Id, o => o.MapFrom(x => x.CategoryId))
.ForMember(x => x.Name, o => o.MapFrom(x => x.Category!.Name))
;
CreateMap<AccountAccessDto, AccessRightsViewModel>();
}
}
/*public static class AccountViewModelExtensions
{
public static AccountViewModel ToModel(this AccountDto input)
{
return new AccountViewModel
{
Id = input.Id.ToShort(),
Name = input.Name,
CurrencyId = input.CurrencyGlobalId,
CurrencyName = input.CurrencyGlobal!.Name,
Categories = input.Categories!
.Select(x => x.Category!.ToModel())
.ToList(),
AccessRights = input.AccessRights!
.Select(x => x.ToModel())
.ToList(),
};
}
}*/
}
@@ -0,0 +1,71 @@
namespace MyOffice.Web.Models.Account;
using AutoMapper;
using MyOffice.Services.Identity;
using Services.Account.Domain;
using Services.Mapper;
using System.Security.Cryptography;
using Core.Extensions;
using User;
using static MyOffice.Web.Models.Account.AccountAccessViewModel;
public class CustomResolver : IValueResolver<AccountAccessDto, AccessRightsViewModel, bool>
{
private readonly ILogger<CustomResolver> _logger;
private readonly IContextProvider _contextProvider;
public CustomResolver(
ILogger<CustomResolver> logger,
IContextProvider contextProvider
)
{
_logger = logger;
_contextProvider = contextProvider;
}
public bool Resolve(AccountAccessDto source, AccessRightsViewModel destination, bool member, ResolutionContext context)
{
return source.OwnerId == _contextProvider.UserId;
}
}
public class AccountViewModelProfile : Profile
{
public AccountViewModelProfile()
{
CreateMap<AccountDetailedDto, AccountDetailedViewModel>()
.ForMember(x => x.Account, o => o.MapFrom(x => x.Account))
;
CreateMap<UserDto, UserViewModel>();
CreateMap<MyOffice.Data.Models.Users.User, UserViewModel>();
CreateMap<AccountDto, AccountViewModel>()
.ForMember(x => x.CurrencyId, o => o.MapFrom(x => x.CurrencyGlobalId))
.ForMember(x => x.CurrencyName, o => o.MapFrom(x => x.Currency!.Name))
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.HasMotions))
.ForMember(x => x.AllowManage, o => o.MapFrom(x => x.AccessRights!.Any(a => a.OwnerId == x.CurrentUserId)))
.ForMember(x => x.AccessRights, o => o.MapFrom((s, d) => s.OwnerId != s.CurrentUserId ? null : s.AccessRights))
;
CreateMap<AccountAccountCategoryDto, AccountCategoryViewModel>()
.ForMember(x => x.Id, o => o.MapFrom(x => x.CategoryId))
.ForMember(x => x.Name, o => o.MapFrom(x => x.Category!.Name))
;
CreateMap<AccountAccessDto, AccessRightsViewModel>()
.ForMember(x => x.IsAllowDelete, o => o.MapFrom(
(src, dst) => src.Account!.OwnerId == src.Account.CurrentUserId && src.UserId != src.Account.CurrentUserId))
.ForMember(x => x.IsOwner, o => o.MapFrom(
(src, dst) => src.OwnerId == src.UserId))
;
CreateMap<AccountAccessItemViewModel, AccountAccessDto>()
.ForMember(x => x.IsAllowWrite, o => o.MapFrom(x => x.AllowWrite))
;
CreateMap<AccountCategoryDto, AccountCategoryViewModel>()
;
}
}
@@ -0,0 +1,12 @@
namespace MyOffice.Web.Models.Account;
using AutoMapper;
using Core.Extensions;
public class ViewModelProfile : Profile
{
public ViewModelProfile()
{
CreateMap<Guid, string>().ConvertUsing(x => x.ToShort());
}
}
+30 -12
View File
@@ -42,16 +42,28 @@ using MyOffice.Services.Currency;
using Services.Account;
using Services.Item;
using MyOffice.Services.Dashboard;
using Services.Account.Domain;
using MyOffice.Services.Identity;
using AutoMapper;
using Models.Motion;
using MyOffice.Services.Account.Domain;
using MyOffice.Services.Mapper;
//TODO: Data.Model only Repository and Service, response <-> mapper <-> web <-> mapper <-> service <-> repository
//TODO: Project management
//TODO: Model names. Controller = xxxRequest/xxxResponse. Service xxxInput/xxxOutput.
//TODO: Validate string as Guid when id
//TODO: Logging
//TODO: Test back
//TODO: Test front
//TODO: Test DB ???
//TODO: Email confirmation
//TODO: Password recovery
//TODO: Account sharing
//TODO: /dashboard/dashboard -> /dashboard/main or /dashboard
//TODO: SPA load categories twice menu + setting (cache)
//TODO: SPA update account category -> update menu
//TODO: Response model from base type
//TODO: XXXResult -> XXXStatus
//TODO: SPA isAllowDelete -> allowDelete (remove is)
public class Program
{
@@ -79,6 +91,12 @@ public class Program
//File Logger
builder.Logging.AddFile(builder.Configuration.GetSection("Logging"));
builder.Services.Configure<LoggerFilterOptions>(options =>
{
//options.AddFilter("IdentityServer4", LogLevel.Warning); // or LogLevel.None to completely disable
options.AddFilter("IdentityServer4", LogLevel.None); // or LogLevel.None to completely disable
});
// shared configuration
builder.Configuration.AddConfiguration(SharedConfiguration.CreateConfigurationContainer());
@@ -180,9 +198,8 @@ public class Program
o.UserInteraction = new UserInteractionOptions()
{
LoginUrl = "/authentication/signin",
LoginReturnUrlParameter = "returnUrl"
LoginReturnUrlParameter = "returnUrl",
};
//o.IssuerUri = "http://localhost:9300";
})
.AddSigningCredential(CreateSigningCredential(builder))
.AddPersistedGrantStore<PersistedGrantStore>()
@@ -222,14 +239,15 @@ public class Program
private static void AddMapping(WebApplicationBuilder builder)
{
builder.Services.AddSingleton(provider => new MapperConfiguration(cfg =>
{
cfg.AddProfile(new AccountServiceProfile());
cfg.AddProfile(new ViewModelProfile());
cfg.AddProfile(new AccountViewModelProfile(provider.CreateScope().ServiceProvider.GetService<IContextProvider>()!));
cfg.AddProfile(new AccountControllerProfile());
}).CreateMapper());
builder.Services.AddAutoMapper(
c =>
{
c.AllowNullCollections = true;
c.AllowNullDestinationValues = true;
},
typeof(AccountViewModelProfile),
typeof(AccountServiceProfile)
);
}
private static void AddRepositories(WebApplicationBuilder builder)