refactoring

This commit is contained in:
2023-06-22 10:01:27 +03:00
parent e9d4053e65
commit d549877567
116 changed files with 1216 additions and 11709 deletions
@@ -61,7 +61,7 @@ public class AccountController : BaseApiController
[HttpGet("~/api/accounts/{id}/motions")]
public object AccountMotionsGet(string id, [FromQuery] AccountMotionsGetModel request)
{
var exec = _accountService.GetAccountMotion(UserId, id!.AsGuid(), request.From.StartOfDay(), request.To.EndOfDay());
var exec = _accountService.GetMotions(UserId, id!.AsGuid(), request.From.StartOfDay(), request.To.EndOfDay());
switch (exec.Status)
{
@@ -78,17 +78,17 @@ public class AccountController : BaseApiController
}
[HttpPost("~/api/accounts/{id}/motions")]
public object AccountsMotionsPost(string id, AccountMotionRequest accountMotion)
public object AccountsMotionsPost(string id, MotionRequest motion)
{
var exec = _accountService.AccountMotionAdd(UserId, id.AsGuid(), accountMotion.FromModel());
var exec = _accountService.MotionAdd(UserId, id.AsGuid(), motion.FromModel());
switch (exec.Status)
{
case AccountMotionAddResult.account_not_found:
case MotionAddResult.account_not_found:
return ProblemBadRequest("Account not found.");
case AccountMotionAddResult.failure:
case MotionAddResult.failure:
return ProblemBadRequest("Adding motion failed.");
case AccountMotionAddResult.success:
case MotionAddResult.success:
return exec.Result!.ToModel();
default:
@@ -1,127 +0,0 @@
namespace MyOffice.Web.Controllers;
using Data.Repositories.Account;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Models.Motion;
using MyOffice.Core.Extensions;
using MyOffice.Core;
using MyOffice.Data.Repositories.Motion;
using MyOffice.Services.Account;
using MyOffice.Services.Account.Domain;
using Services.Motion;
using MyOffice.Data.Models.Motions;
using Services.Motion.Domain;
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class SettingsMotionController : BaseApiController
{
private MotionService _motionService;
public SettingsMotionController(
MotionService motionService
)
{
_motionService = motionService;
}
[HttpGet("~/api/settings/motion-categories")]
public object MotionCategories()
{
var list = _motionService.GetAllCategories(UserId);
return list
.Select(x => x.ToModel())
.OrderByDescending(x => x.SortOrder)
.ThenBy(x => x.Name);
}
[HttpPost("~/api/settings/motion-categories")]
public object MotionCategoriesAdd(MotionCategoryViewModel request)
{
var exec = _motionService.CategoryAdd(UserId, new MotionCategory { Name = request.Name! });
switch (exec.Status)
{
case GeneralExecStatus.success:
return exec.Result!.ToModel();
case GeneralExecStatus.failure:
case GeneralExecStatus.not_found:
return ProblemBadRequest("Adding motion category failed.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpPut("~/api/settings/motion-categories/{id}")]
public object MotionCategoriesEdit(string id, MotionCategoryViewModel request)
{
var exec = _motionService.CategoryUpdate(UserId, id.AsGuid(), new MotionCategory { Name = request.Name! });
switch (exec.Status)
{
case GeneralExecStatus.success:
return exec.Result!.ToModel();
case GeneralExecStatus.failure:
case GeneralExecStatus.not_found:
return ProblemBadRequest("Update account category failed.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpDelete("~/api/settings/motion-categories/{id}")]
public object MotionCategoriesDelete(string id)
{
var exec = _motionService.CategoryRemove(UserId, id.AsGuid());
switch (exec.Status)
{
case MotionCategoryRemoveResult.success:
return exec.Result!;
case MotionCategoryRemoveResult.failure:
return ProblemBadRequest("Remove motion category failed.");
case MotionCategoryRemoveResult.not_found:
return ProblemBadRequest("Account motion not found.");
case MotionCategoryRemoveResult.accounts_exists:
return ProblemBadRequest("Account motion have accounts.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpGet("~/api/settings/motions")]
public object Motions(string? category)
{
var list = category.IsMissing()
? _motionService.GetAll(UserId)
: _motionService.GetByCategory(UserId, category!.AsGuid());
return list
.OrderBy(x => x.MotionGlobal.Name)
.Select(x => x.ToModel());
}
[HttpPut("~/api/settings/motions/{id}")]
public object Motions(string id, MotionEditModel request)
{
var exec = _motionService.Update(UserId, id.AsGuid(), request.Category.AsGuid());
switch (exec.Status)
{
case GeneralExecStatus.not_found:
return ProblemBadRequest("Motion not found.");
case GeneralExecStatus.failure:
return ProblemBadRequest("Motion update failed.");
case GeneralExecStatus.success:
return Ok(exec.Result!.ToModel());
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
}
@@ -54,7 +54,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpPost("~/api/settings/account-categories")]
public object AccountCategoriesAdd(MotionCategoryViewModel request)
public object AccountCategoriesAdd(AccountCategoryViewModel request)
{
var exec = _accountService.CategoryAdd(UserId, new AccountCategory { Name = request.Name! });
switch (exec.Status)
@@ -72,7 +72,7 @@ public class SettingsAccountController : BaseApiController
}
[HttpPut("~/api/settings/account-categories/{id}")]
public object AccountCategoriesEdit(string id, MotionCategoryViewModel request)
public object AccountCategoriesEdit(string id, AccountCategoryViewModel request)
{
var exec = _accountService.CategoryUpdate(UserId, id.AsGuid(), new AccountCategory { Name = request.Name! });
switch (exec.Status)
@@ -0,0 +1,126 @@
namespace MyOffice.Web.Controllers;
using Data.Models.Items;
using Data.Repositories.Account;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Models.Item;
using MyOffice.Core.Extensions;
using MyOffice.Core;
using MyOffice.Services.Account;
using MyOffice.Services.Account.Domain;
using Services.Item;
using Services.Item.Domain;
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class SettingsItemController : BaseApiController
{
private ItemService _itemService;
public SettingsItemController(
ItemService itemService
)
{
_itemService = itemService;
}
[HttpGet("~/api/settings/item-categories")]
public object ItemsCategories()
{
var list = _itemService.GetAllCategories(UserId);
return list
.Select(x => x.ToModel())
.OrderByDescending(x => x.SortOrder)
.ThenBy(x => x.Name);
}
[HttpPost("~/api/settings/item-categories")]
public object ItemCategoriesAdd(ItemCategoryViewModel request)
{
var exec = _itemService.CategoryAdd(UserId, new ItemCategory { Name = request.Name! });
switch (exec.Status)
{
case GeneralExecStatus.success:
return exec.Result!.ToModel();
case GeneralExecStatus.failure:
case GeneralExecStatus.not_found:
return ProblemBadRequest("Adding item category failed.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpPut("~/api/settings/item-categories/{id}")]
public object ItemCategoriesEdit(string id, ItemCategoryViewModel request)
{
var exec = _itemService.CategoryUpdate(UserId, id.AsGuid(), new ItemCategory { Name = request.Name! });
switch (exec.Status)
{
case GeneralExecStatus.success:
return exec.Result!.ToModel();
case GeneralExecStatus.failure:
case GeneralExecStatus.not_found:
return ProblemBadRequest("Update item category failed.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpDelete("~/api/settings/item-categories/{id}")]
public object ItemCategoriesDelete(string id)
{
var exec = _itemService.CategoryRemove(UserId, id.AsGuid());
switch (exec.Status)
{
case ItemCategoryRemoveResult.success:
return exec.Result!;
case ItemCategoryRemoveResult.failure:
return ProblemBadRequest("Remove item category failed.");
case ItemCategoryRemoveResult.not_found:
return ProblemBadRequest("Account item not found.");
case ItemCategoryRemoveResult.accounts_exists:
return ProblemBadRequest("Account motion have accounts.");
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
[HttpGet("~/api/settings/items")]
public object Items(string? category)
{
var list = category.IsMissing()
? _itemService.GetAll(UserId)
: _itemService.GetByCategory(UserId, category!.AsGuid());
return list
.OrderBy(x => x.ItemGlobal.Name)
.Select(x => x.ToModel());
}
[HttpPut("~/api/settings/items/{id}")]
public object Items(string id, ItemEditModel request)
{
var exec = _itemService.Update(UserId, id.AsGuid(), request.Category.AsGuid());
switch (exec.Status)
{
case GeneralExecStatus.not_found:
return ProblemBadRequest("Item not found.");
case GeneralExecStatus.failure:
return ProblemBadRequest("Item update failed.");
case GeneralExecStatus.success:
return Ok(exec.Result!.ToModel());
default:
throw new NotSupportedException(exec.Status.ToString());
}
}
}
@@ -4,7 +4,7 @@
using Core.Extensions;
using Data.Models.Accounts;
public class MotionCategoryViewModel
public class AccountCategoryViewModel
{
public string? Id { get; set; }
@@ -16,9 +16,9 @@
public static class AccountCategoryViewModelExtensions
{
public static MotionCategoryViewModel ToModel(this AccountCategory input)
public static AccountCategoryViewModel ToModel(this AccountCategory input)
{
return new MotionCategoryViewModel
return new AccountCategoryViewModel
{
Id = input.Id.ToShort(),
Name = input.Name,
@@ -1,4 +1,4 @@
namespace MyOffice.Web.Models.Motion
namespace MyOffice.Web.Models.Account
{
using System.ComponentModel.DataAnnotations;
@@ -3,7 +3,6 @@
using System.ComponentModel.DataAnnotations;
using Core.Extensions;
using Data.Models.Accounts;
using User;
public class AccountViewModel
{
@@ -16,7 +15,7 @@
public string CurrencyId { get; set; } = null!;
public string? CurrencyName { get; set; }
public List<MotionCategoryViewModel>? Categories { get; set; }
public List<AccountCategoryViewModel>? Categories { get; set; }
[Required]
public string CategoryId { get; set; } = null!;
@@ -1,43 +0,0 @@
namespace MyOffice.Web.Models.AccountMotion;
using Data.Models.Accounts;
using Services.Account.Domain;
public class AccountMotionViewModel
{
public string? Id { get; set; }
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string? Description { get; set; }
public decimal Plus { get; set; }
public decimal Minus { get; set; }
}
public class AccountMotionRequest
{
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string? Description { get; set; }
public decimal? Plus { get; set; }
public decimal? Minus { get; set; }
}
public static class AccountMotionViewModelExtension
{
}
public static class AccountMotionRequestExtension
{
public static AccountMotionAdd FromModel(this AccountMotionRequest input)
{
return new AccountMotionAdd
{
Date = input.Date,
Motion = input.Motion,
Minus = input.Minus ?? 0,
Plus = input.Plus ?? 0,
Description = input.Description,
};
}
}
@@ -0,0 +1,31 @@
namespace MyOffice.Web.Models.Item
{
using System.ComponentModel.DataAnnotations;
using MyOffice.Core.Extensions;
using MyOffice.Data.Models.Items;
public class ItemCategoryViewModel
{
public string? Id { get; set; }
[Required]
public string? Name { get; set; }
public bool AllowDelete { get; set; }
public int SortOrder { get; set; }
}
public static class ItemCategoryViewModelExtensions
{
public static ItemCategoryViewModel ToModel(this ItemCategory input)
{
return new ItemCategoryViewModel
{
Id = input.Id.ToShort(),
Name = input.Name,
AllowDelete = !input.Items.Any() && input.Id != input.UserId,
SortOrder = input.Id == input.UserId ? 1 : 0
};
}
}
}
@@ -1,6 +1,6 @@
namespace MyOffice.Web.Models.Motion
namespace MyOffice.Web.Models.Item
{
public class MotionEditModel
public class ItemEditModel
{
public string Id { get; set; } = null!;
public string Category { get; set; } = null!;
+33
View File
@@ -0,0 +1,33 @@
namespace MyOffice.Web.Models.Item
{
using System.ComponentModel.DataAnnotations;
using MyOffice.Core.Extensions;
using MyOffice.Data.Models.Items;
public class ItemViewModel
{
public string? Id { get; set; }
public string CategoryId { get; set; } = null!;
public string Category { get; set; } = null!;
[Required]
public string? Name { get; set; }
public bool AllowDelete { get; set; }
}
public static class ItemViewModelExtensions
{
public static ItemViewModel ToModel(this Item input)
{
return new ItemViewModel
{
Id = input.ItemGlobalId.ToShort(),
CategoryId = input.Category.Id.ToShort(),
Category = input.Category.Name,
Name = input.ItemGlobal.Name,
AllowDelete = !input.Motions.Any(),
};
}
}
}
@@ -1,33 +0,0 @@
namespace MyOffice.Web.Models.Motion
{
using System.ComponentModel.DataAnnotations;
using Core.Extensions;
using Data.Models.Accounts;
using MyOffice.Data.Models.Motions;
public class AccountMotionViewModel
{
public string Id { get; set; } = null!;
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string? Description { get; set; }
public decimal Plus { get; set; }
public decimal Minus { get; set; }
}
public static class AccountMotionViewModelExtensions
{
public static AccountMotionViewModel ToModel(this AccountMotion input)
{
return new AccountMotionViewModel
{
Id = input.Id.ToShort(),
Date = input.DateTime,
Motion = input.Motion.MotionGlobal.Name,
Description = input.Description,
Plus = input.AmountPlus,
Minus = input.AmountMinus,
};
}
}
}
@@ -1,33 +0,0 @@
namespace MyOffice.Web.Models.Motion
{
using System.ComponentModel.DataAnnotations;
using Core.Extensions;
using Data.Models.Accounts;
using MyOffice.Data.Models.Motions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
public class MotionCategoryViewModel
{
public string? Id { get; set; }
[Required]
public string? Name { get; set; }
public bool AllowDelete { get; set; }
public int SortOrder { get; set; }
}
public static class MotionCategoryViewModelExtensions
{
public static MotionCategoryViewModel ToModel(this MotionCategory input)
{
return new MotionCategoryViewModel
{
Id = input.Id.ToShort(),
Name = input.Name,
AllowDelete = !input.Motions.Any() && input.Id != input.UserId,
SortOrder = input.Id == input.UserId ? 1 : 0
};
}
}
}
@@ -0,0 +1,27 @@
namespace MyOffice.Web.Models.Motion;
using MyOffice.Services.Account.Domain;
public class MotionRequest
{
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string? Description { get; set; }
public decimal? Plus { get; set; }
public decimal? Minus { get; set; }
}
public static class MotionRequestExtension
{
public static MotionAdd FromModel(this MotionRequest input)
{
return new MotionAdd
{
Date = input.Date,
Motion = input.Motion,
Minus = input.Minus ?? 0,
Plus = input.Plus ?? 0,
Description = input.Description,
};
}
}
+15 -17
View File
@@ -1,32 +1,30 @@
namespace MyOffice.Web.Models.Motion
namespace MyOffice.Web.Models.AccountMotion
{
using System.ComponentModel.DataAnnotations;
using Core.Extensions;
using MyOffice.Data.Models.Motions;
using Data.Models.Accounts;
public class MotionViewModel
{
public string? Id { get; set; }
public string CategoryId { get; set; } = null!;
public string Category { get; set; } = null!;
[Required]
public string? Name { get; set; }
public bool AllowDelete { get; set; }
public string Id { get; set; } = null!;
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string? Description { get; set; }
public decimal Plus { get; set; }
public decimal Minus { get; set; }
}
public static class MotionViewModelExtensions
{
public static MotionViewModel ToModel(this MotionAccount input)
public static MotionViewModel ToModel(this Motion input)
{
return new MotionViewModel
{
Id = input.MotionGlobalId.ToShort(),
CategoryId = input.Category.Id.ToShort(),
Category = input.Category.Name,
Name = input.MotionGlobal.Name,
AllowDelete = !input.Motions.Any(),
Id = input.Id.ToShort(),
Date = input.DateTime,
Motion = input.Item.ItemGlobal.Name,
Description = input.Description,
Plus = input.AmountPlus,
Minus = input.AmountMinus,
};
}
}
+6 -6
View File
@@ -28,6 +28,7 @@ using System.Text.Json.Serialization;
using Core.Identity;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using Data.Repositories.Item;
using Data.Repositories.Users;
using DbContext;
using Services.Users;
@@ -38,8 +39,7 @@ using Microsoft.Extensions.Logging.Console;
using IdentityServer4.Services;
using MyOffice.Services.Currency;
using Services.Account;
using MyOffice.Data.Repositories.Motion;
using Services.Motion;
using Services.Item;
public class Program
{
@@ -217,10 +217,10 @@ public class Program
builder.Services.AddScoped<IAccountRepository, AccountRepository>();
builder.Services.AddScoped<IAccountAccountCategoryRepository, AccountAccountCategoryRepository>();
builder.Services.AddScoped<IMotionCategoryRepository, MotionCategoryRepository>();
builder.Services.AddScoped<IItemCategoryRepository, ItemCategoryRepository>();
builder.Services.AddScoped<IItemRepository, ItemRepository>();
builder.Services.AddScoped<IItemGlobalRepository, ItemGlobalRepository>();
builder.Services.AddScoped<IMotionRepository, MotionRepository>();
builder.Services.AddScoped<IMotionGlobalRepository, MotionGlobalRepository>();
builder.Services.AddScoped<IAccountMotionRepository, AccountMotionRepository>();
}
private static void AddBusinessServices(WebApplicationBuilder builder)
@@ -228,7 +228,7 @@ public class Program
builder.Services.AddScoped<UserService, UserService>();
builder.Services.AddScoped<CurrencyService, CurrencyService>();
builder.Services.AddScoped<AccountService, AccountService>();
builder.Services.AddScoped<MotionService, MotionService>();
builder.Services.AddScoped<ItemService, ItemService>();
}
private static readonly ConcurrentDictionary<string, PhysicalFileInfo> _staticFilesCache = new();