| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Domain; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 12 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 13 | | using WinSolutions.Sveta.Common; |
| | | 14 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 15 | | |
| | | 16 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 17 | | { |
| | | 18 | | public class GoodLabelService : SvetaServiceBase, IGoodLabelService |
| | | 19 | | { |
| | | 20 | | private readonly SvetaDbContext _db; |
| | | 21 | | private readonly ILogger<GoodLabelService> _logger; |
| | | 22 | | private readonly IAuthenticationService _authenticationService; |
| | | 23 | | public GoodLabelService(SvetaDbContext db, ILogger<GoodLabelService> logger, IAuthenticationService authenticati |
| | 2 | 24 | | : base(authenticationService) |
| | 2 | 25 | | { |
| | 2 | 26 | | _authenticationService = authenticationService; |
| | 2 | 27 | | _db = db; |
| | 2 | 28 | | _logger = logger; |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// получает список ярлыков товаров |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="page">номер страницы, начинается с 0</param> |
| | | 35 | | /// <param name="limit">Кол-во записей</param> |
| | | 36 | | /// <param name="filter">фильтр по названию ярлыка</param> |
| | | 37 | | /// <param name="sort">сортировка</param> |
| | | 38 | | /// <param name="showcaseVisible">видимость на витрине</param> |
| | | 39 | | /// <returns></returns> |
| | | 40 | | public async Task<PaginatedData<List<GoodLabel>>> GetGoodLabels(int page, int limit, string sort, string filter, |
| | 6 | 41 | | { |
| | 6 | 42 | | var labels = PrepareGoodLabels().AsNoTracking(); |
| | 6 | 43 | | int total = await labels?.CountAsync(); |
| | 6 | 44 | | labels = labels.Where(x => string.IsNullOrWhiteSpace(filter) || x.Name.ToLower().Contains(filter.ToLower())) |
| | 6 | 45 | | .Where(x=> !showcaseVisible.HasValue || (showcaseVisible.Value ? x.ShowcaseVisible : !x.ShowcaseVisible) |
| | 6 | 46 | | int filtered = await labels?.CountAsync(); |
| | 6 | 47 | | labels = Sort(labels, sort?.ToLower()); |
| | 6 | 48 | | return new PaginatedData<List<GoodLabel>> |
| | 6 | 49 | | { |
| | 6 | 50 | | Result = await labels.Skip(page * limit).Take(limit).ToListAsync(), |
| | 6 | 51 | | TotalCount = total, |
| | 6 | 52 | | TotalFilteredCount = filtered |
| | 6 | 53 | | }; |
| | 6 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Подготовительный метод, выбирает все записи из GoodLabels |
| | | 58 | | /// </summary> |
| | | 59 | | /// <returns></returns> |
| | 9 | 60 | | private IQueryable<GoodLabel> PrepareGoodLabels() => _db.GoodLabels |
| | 9 | 61 | | .Where(e => !e.IsDeleted) |
| | 9 | 62 | | .AsQueryable(); |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Получает запись по id |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="id">id записи</param> |
| | | 68 | | /// <returns></returns> |
| | 1 | 69 | | public async Task<GoodLabel> GetGoodLabel(long id) => await PrepareGoodLabels().FirstOrDefaultAsync(e => e.Id == |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Создает запись |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="data">объект GoodLabel</param> |
| | | 75 | | /// <returns></returns> |
| | | 76 | | public async Task Create(GoodLabel data) |
| | 1 | 77 | | { |
| | 1 | 78 | | await _db.GoodLabels.AddAsync(data); |
| | 1 | 79 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Проверяет существует ли запись |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="id">id записи</param> |
| | | 86 | | /// <returns></returns> |
| | 2 | 87 | | public async Task<bool> GoodLabelExists(long id) => await PrepareGoodLabels().AsNoTracking().AnyAsync(e => e.Id |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Обновляет запись |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="data">объект GoodLabel</param> |
| | | 93 | | /// <returns></returns> |
| | | 94 | | public async Task Update(GoodLabel data) |
| | 1 | 95 | | { |
| | 1 | 96 | | if (!(await GoodLabelExists(data.Id))) |
| | 0 | 97 | | { |
| | 0 | 98 | | throw new ArgumentException($"Ярлык с id={data.Id} не найден"); |
| | | 99 | | } |
| | 1 | 100 | | _db.GoodLabels.Update(data); |
| | 1 | 101 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Удаляет запись |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="id">id записи</param> |
| | | 108 | | /// <returns></returns> |
| | | 109 | | public async Task Delete(long id) |
| | 1 | 110 | | { |
| | 1 | 111 | | var rec = await _db.GoodLabels.FindAsync(id); |
| | 1 | 112 | | if (rec == null) |
| | 0 | 113 | | throw new KeyNotFoundException($"Ярлык с id={id} не найден"); |
| | 1 | 114 | | rec.IsDeleted = true; |
| | 1 | 115 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Получить запись |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="name">имя ярлыка</param> |
| | | 122 | | /// <returns></returns> |
| | | 123 | | public async Task<GoodLabel> GetGoodLabelByName(string name) |
| | 0 | 124 | | => await PrepareGoodLabels().FirstOrDefaultAsync(x => x.Name.ToLower() == name.NormalizeName().ToLower()); |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// сортировка по имени и id |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="items">выборка ярлыков</param> |
| | | 130 | | /// <param name="sort">тип сортировки</param> |
| | | 131 | | /// <returns></returns> |
| | 6 | 132 | | private IQueryable<GoodLabel> Sort(IQueryable<GoodLabel> items, string sort = default) => (sort ?? "").ToLower() |
| | 6 | 133 | | { |
| | 6 | 134 | | "name|desc" => items.OrderByDescending(d => d.Name), |
| | 6 | 135 | | "id|desc" => items.OrderByDescending(d => d.Id), |
| | 6 | 136 | | "id" => items.OrderBy(d => d.Id), |
| | 6 | 137 | | _ => items.OrderBy(d => d.Name) |
| | 6 | 138 | | }; |
| | | 139 | | } |
| | | 140 | | } |