< Summary

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

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetPhoto()0%100%
GetPhotos()0%100%
Create()0%0%
UpdatePhoto()0%0%
Delete()0%0%
PhotoExists()0%100%
FindPhotos(...)0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/PhotoService.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 PhotoService : SvetaServiceBase, IPhotoService
 16    {
 17        private readonly SvetaDbContext _db;
 18        private readonly ILogger<PhotoService> _logger;
 19        public PhotoService(SvetaDbContext db, ILogger<PhotoService> logger, IAuthenticationService authenticationServic
 020            : base(authenticationService)
 021        {
 022            _db = db;
 023            _logger = logger;
 024        }
 025        public async Task<Photo> GetPhoto(long id) => await _db.Photos
 026            .Include(d => d.RecState)
 027            .Where(d => !d.IsDeleted)
 028            .FirstAsync(f=>f.Id == id);
 029        public async Task<List<Photo>> GetPhotos(int page, int limit) => await _db.Photos
 030            .AsNoTracking()
 031            .Include(d => d.RecState)
 032            .Where(d => !d.IsDeleted)
 033            .Skip(page * limit)
 034            .Take(limit)
 035            .ToListAsync();
 36        public async Task Create(Photo photo)
 037        {
 038            if (photo.RecState != null)
 039                _db.Entry(photo.RecState).State = photo.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged;
 040            await _db.Photos.AddAsync(photo);
 041            await _db.SaveChangesAsync(CurrentUserId);
 042        }
 43        public async Task UpdatePhoto(Photo data)
 044        {
 045            if (!(await PhotoExists(data.Id)))
 046            {
 047                throw new ArgumentException($"Record #{data.Id} not found");
 48            }
 049            _db.Photos.Update(data);
 050            await _db.SaveChangesAsync(CurrentUserId);
 051        }
 52        public async Task Delete(long id)
 053        {
 054            Photo photo = await _db.Photos
 055                .Where(d => !d.IsDeleted)
 056                .FirstAsync(f => f.Id == id);
 057            if (photo == null)
 058                throw new KeyNotFoundException($"Record #{id} not found");
 059            photo.IsDeleted = true;
 060            await _db.SaveChangesAsync(CurrentUserId);
 061        }
 062        public async Task<bool> PhotoExists(long id) => await _db.Photos
 063            .Where(d => d.Id == id)
 064            .Where(d => !d.IsDeleted)
 065            .AnyAsync();
 66
 67        public List<Photo> FindPhotos(List<string> fullSizeUrls)
 068        {
 069            return _db.Photos.Where(x => !x.IsDeleted && fullSizeUrls.Contains(x.FullSizeUrl.ToLower())).ToList();
 070        }
 71    }
 72}