| | | 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; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | using WinSolutions.Sveta.Common; |
| | | 12 | | |
| | | 13 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 14 | | { |
| | | 15 | | public class PromoOfferService : SvetaServiceBase, IPromoOfferService |
| | | 16 | | { |
| | | 17 | | private readonly SvetaDbContext _db; |
| | | 18 | | private readonly ILogger<PromoOfferService> _logger; |
| | | 19 | | readonly IAuthenticationService _authenticationService; |
| | | 20 | | |
| | | 21 | | public PromoOfferService(SvetaDbContext db, ILogger<PromoOfferService> logger, IAuthenticationService authentica |
| | 0 | 22 | | : base(authenticationService) |
| | 0 | 23 | | { |
| | 0 | 24 | | _db = db; |
| | 0 | 25 | | _logger = logger; |
| | 0 | 26 | | _authenticationService = authenticationService; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task Create(PromoOffer data) |
| | 0 | 30 | | { |
| | 0 | 31 | | if (data.RecState != null) |
| | 0 | 32 | | _db.Entry(data.RecState).State = data.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged; |
| | 0 | 33 | | await _db.PromoOffers.AddAsync(data); |
| | 0 | 34 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task Delete(long id) |
| | 0 | 38 | | { |
| | 0 | 39 | | var data = await _db.PromoOffers |
| | 0 | 40 | | .Where(d => !d.IsDeleted) |
| | 0 | 41 | | .FirstAsync(pf=>pf.Id == id); |
| | 0 | 42 | | if (data == null) |
| | 0 | 43 | | { |
| | 0 | 44 | | throw new KeyNotFoundException($"Record #{id} not found"); |
| | | 45 | | } |
| | 0 | 46 | | data.IsDeleted = true; |
| | 0 | 47 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public async Task<PromoOffer> GetPromoOffer(long id) => await _db.PromoOffers |
| | 0 | 51 | | .Include(x => x.SupplierDepartment) |
| | 0 | 52 | | .Include(x => x.Good) |
| | 0 | 53 | | .Include(d => d.RecState) |
| | 0 | 54 | | .Where(d => !d.IsDeleted) |
| | 0 | 55 | | .AsQueryable() |
| | 0 | 56 | | .FirstOrDefaultAsync(x => x.Id == id); |
| | | 57 | | |
| | 0 | 58 | | public async Task<PromoOffer> GetPromoOfferDetailed(long id) => await _db.PromoOffers |
| | 0 | 59 | | .Include(x => x.SupplierDepartment) |
| | 0 | 60 | | .Include(x => x.Good) |
| | 0 | 61 | | .Include(d => d.RecState) |
| | 0 | 62 | | .Include(x => x.Bids) |
| | 0 | 63 | | .Where(d => !d.IsDeleted) |
| | 0 | 64 | | .AsQueryable() |
| | 0 | 65 | | .FirstOrDefaultAsync(x => x.Id == id); |
| | | 66 | | |
| | | 67 | | public async Task<List<PromoOffer>> GetPromoOffers(int page, int? limit) |
| | 0 | 68 | | { |
| | 0 | 69 | | var data = _db.PromoOffers |
| | 0 | 70 | | .Include(x => x.SupplierDepartment) |
| | 0 | 71 | | .Include(x => x.Bids) |
| | 0 | 72 | | .Include(d => d.RecState) |
| | 0 | 73 | | .AsNoTracking() |
| | 0 | 74 | | .Where(d => !d.IsDeleted) |
| | 0 | 75 | | .OrderByDescending(x => x.DateBegin); |
| | 0 | 76 | | if (limit != null) |
| | 0 | 77 | | { |
| | 0 | 78 | | var result = data.Skip(page * (int)limit).Take((int)limit); |
| | 0 | 79 | | return await result.ToListAsync(); |
| | | 80 | | } |
| | 0 | 81 | | return await data.ToListAsync(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | public async Task<bool> PromoOfferExists(long id) => await _db.PromoOffers |
| | 0 | 85 | | .Where(e => e.Id == id) |
| | 0 | 86 | | .Where(d => !d.IsDeleted) |
| | 0 | 87 | | .AnyAsync(); |
| | | 88 | | } |
| | | 89 | | } |