< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.PromoOfferService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/PromoOfferService.cs
Covered lines:0
Uncovered lines:55
Coverable lines:55
Total lines:89
Line coverage:0% (0 of 55)
Covered branches:0
Total branches:16
Branch coverage:0% (0 of 16)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
Create()0%0%
Delete()0%0%
GetPromoOffer()0%100%
GetPromoOfferDetailed()0%100%
GetPromoOffers()0%0%
PromoOfferExists()0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/PromoOfferService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Text;
 7using System.Threading.Tasks;
 8using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 9using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 10using WinSolutions.Sveta.Server.Services.Interfaces;
 11using WinSolutions.Sveta.Common;
 12
 13namespace 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
 022            : base(authenticationService)
 023        {
 024            _db = db;
 025            _logger = logger;
 026            _authenticationService = authenticationService;
 027        }
 28
 29        public async Task Create(PromoOffer data)
 030        {
 031            if (data.RecState != null)
 032                _db.Entry(data.RecState).State = data.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged;
 033            await _db.PromoOffers.AddAsync(data);
 034            await _db.SaveChangesAsync(CurrentUserId);
 035        }
 36
 37        public async Task Delete(long id)
 038        {
 039            var data = await _db.PromoOffers
 040                .Where(d => !d.IsDeleted)
 041                .FirstAsync(pf=>pf.Id == id);
 042            if (data == null)
 043            {
 044                throw new KeyNotFoundException($"Record #{id} not found");
 45            }
 046            data.IsDeleted = true;
 047            await _db.SaveChangesAsync(CurrentUserId);
 048        }
 49
 050        public async Task<PromoOffer> GetPromoOffer(long id) => await _db.PromoOffers
 051            .Include(x => x.SupplierDepartment)
 052            .Include(x => x.Good)
 053            .Include(d => d.RecState)
 054            .Where(d => !d.IsDeleted)
 055            .AsQueryable()
 056            .FirstOrDefaultAsync(x => x.Id == id);
 57
 058        public async Task<PromoOffer> GetPromoOfferDetailed(long id) => await _db.PromoOffers
 059            .Include(x => x.SupplierDepartment)
 060            .Include(x => x.Good)
 061            .Include(d => d.RecState)
 062            .Include(x => x.Bids)
 063            .Where(d => !d.IsDeleted)
 064            .AsQueryable()
 065            .FirstOrDefaultAsync(x => x.Id == id);
 66
 67        public async Task<List<PromoOffer>> GetPromoOffers(int page, int? limit)
 068        {
 069            var data = _db.PromoOffers
 070              .Include(x => x.SupplierDepartment)
 071              .Include(x => x.Bids)
 072              .Include(d => d.RecState)
 073              .AsNoTracking()
 074              .Where(d => !d.IsDeleted)
 075              .OrderByDescending(x => x.DateBegin);
 076            if (limit != null)
 077            {
 078                var result = data.Skip(page * (int)limit).Take((int)limit);
 079                return await result.ToListAsync();
 80            }
 081            return await data.ToListAsync();
 082        }
 83
 084        public async Task<bool> PromoOfferExists(long id) => await _db.PromoOffers
 085            .Where(e => e.Id == id)
 086            .Where(d => !d.IsDeleted)
 087            .AnyAsync();
 88    }
 89}