| | | 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 PromoBidService : SvetaServiceBase, IPromoBidService |
| | | 16 | | { |
| | | 17 | | private readonly SvetaDbContext _db; |
| | | 18 | | private readonly ILogger<PromoBidService> _logger; |
| | | 19 | | |
| | | 20 | | public PromoBidService(SvetaDbContext db, ILogger<PromoBidService> logger, IAuthenticationService authentication |
| | 0 | 21 | | : base(authenticationService) |
| | 0 | 22 | | { |
| | 0 | 23 | | _db = db; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task Create(PromoBid data) |
| | 0 | 28 | | { |
| | 0 | 29 | | if (data.RecState != null) |
| | 0 | 30 | | _db.Entry(data.RecState).State = data.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged; |
| | 0 | 31 | | await _db.PromoBids.AddAsync(data); |
| | 0 | 32 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task Delete(long id) |
| | 0 | 36 | | { |
| | 0 | 37 | | var data = await _db.PromoBids |
| | 0 | 38 | | .Where(d => !d.IsDeleted) |
| | 0 | 39 | | .FirstAsync(p=>p.Id == id); |
| | 0 | 40 | | if (data == null) |
| | 0 | 41 | | { |
| | 0 | 42 | | throw new KeyNotFoundException($"Record #{id} not found"); |
| | | 43 | | } |
| | 0 | 44 | | data.IsDeleted = true; |
| | 0 | 45 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | public async Task<PromoBid> GetPromoBid(long id) => await _db.PromoBids |
| | 0 | 49 | | .Include(d => d.RecState) |
| | 0 | 50 | | .Where(d => !d.IsDeleted) |
| | 0 | 51 | | .FirstAsync(p => p.Id == id); |
| | | 52 | | |
| | | 53 | | public async Task<List<PromoBid>> GetPromoBids(int page, int? limit) |
| | 0 | 54 | | { |
| | 0 | 55 | | var data = _db.PromoBids |
| | 0 | 56 | | .AsNoTracking() |
| | 0 | 57 | | .Include(d => d.RecState) |
| | 0 | 58 | | .Where(d => !d.IsDeleted); |
| | | 59 | | |
| | 0 | 60 | | if (limit != null) |
| | 0 | 61 | | { |
| | 0 | 62 | | data = data |
| | 0 | 63 | | .Skip(page * (int)limit) |
| | 0 | 64 | | .Take((int)limit); |
| | 0 | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | return await data.ToListAsync(); |
| | 0 | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | public async Task<bool> PromoBidExists(long id) => await _db.PromoBids |
| | 0 | 71 | | .Where(e => e.Id == id) |
| | 0 | 72 | | .Where(d => !d.IsDeleted) |
| | 0 | 73 | | .AnyAsync(); |
| | | 74 | | |
| | | 75 | | public async Task Update(PromoBid data) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (!(await PromoBidExists(data.Id))) |
| | 0 | 78 | | { |
| | 0 | 79 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 80 | | } |
| | 0 | 81 | | _db.PromoBids.Update(data); |
| | 0 | 82 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |