< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.PromoBidService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/PromoBidService.cs
Covered lines:0
Uncovered lines:49
Coverable lines:49
Total lines:85
Line coverage:0% (0 of 49)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
Create()0%0%
Delete()0%0%
GetPromoBid()0%100%
GetPromoBids()0%0%
PromoBidExists()0%100%
Update()0%0%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/PromoBidService.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 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
 021            : base(authenticationService)
 022        {
 023            _db = db;
 024            _logger = logger;
 025        }
 26
 27        public async Task Create(PromoBid data)
 028        {
 029            if (data.RecState != null)
 030                _db.Entry(data.RecState).State = data.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged;
 031            await _db.PromoBids.AddAsync(data);
 032            await _db.SaveChangesAsync(CurrentUserId);
 033        }
 34
 35        public async Task Delete(long id)
 036        {
 037            var data = await _db.PromoBids
 038                .Where(d => !d.IsDeleted)
 039                .FirstAsync(p=>p.Id == id);
 040            if (data == null)
 041            {
 042                throw new KeyNotFoundException($"Record #{id} not found");
 43            }
 044            data.IsDeleted = true;
 045            await _db.SaveChangesAsync(CurrentUserId);
 046        }
 47
 048        public async Task<PromoBid> GetPromoBid(long id) => await _db.PromoBids
 049            .Include(d => d.RecState)
 050            .Where(d => !d.IsDeleted)
 051            .FirstAsync(p => p.Id == id);
 52
 53        public async Task<List<PromoBid>> GetPromoBids(int page, int? limit)
 054        {
 055            var data = _db.PromoBids
 056                .AsNoTracking()
 057                .Include(d => d.RecState)
 058                .Where(d => !d.IsDeleted);
 59
 060            if (limit != null)
 061            {
 062                data = data
 063                    .Skip(page * (int)limit)
 064                    .Take((int)limit);
 065            }
 66
 067            return await data.ToListAsync();
 068        }
 69
 070        public async Task<bool> PromoBidExists(long id) => await _db.PromoBids
 071            .Where(e => e.Id == id)
 072            .Where(d => !d.IsDeleted)
 073            .AnyAsync();
 74
 75        public async Task Update(PromoBid data)
 076        {
 077            if (!(await PromoBidExists(data.Id)))
 078            {
 079                throw new ArgumentException($"Record #{data.Id} not found");
 80            }
 081            _db.PromoBids.Update(data);
 082            await _db.SaveChangesAsync(CurrentUserId);
 083        }
 84    }
 85}