| | | 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 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 |
| | 0 | 20 | | : base(authenticationService) |
| | 0 | 21 | | { |
| | 0 | 22 | | _db = db; |
| | 0 | 23 | | _logger = logger; |
| | 0 | 24 | | } |
| | 0 | 25 | | public async Task<Photo> GetPhoto(long id) => await _db.Photos |
| | 0 | 26 | | .Include(d => d.RecState) |
| | 0 | 27 | | .Where(d => !d.IsDeleted) |
| | 0 | 28 | | .FirstAsync(f=>f.Id == id); |
| | 0 | 29 | | public async Task<List<Photo>> GetPhotos(int page, int limit) => await _db.Photos |
| | 0 | 30 | | .AsNoTracking() |
| | 0 | 31 | | .Include(d => d.RecState) |
| | 0 | 32 | | .Where(d => !d.IsDeleted) |
| | 0 | 33 | | .Skip(page * limit) |
| | 0 | 34 | | .Take(limit) |
| | 0 | 35 | | .ToListAsync(); |
| | | 36 | | public async Task Create(Photo photo) |
| | 0 | 37 | | { |
| | 0 | 38 | | if (photo.RecState != null) |
| | 0 | 39 | | _db.Entry(photo.RecState).State = photo.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged; |
| | 0 | 40 | | await _db.Photos.AddAsync(photo); |
| | 0 | 41 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 42 | | } |
| | | 43 | | public async Task UpdatePhoto(Photo data) |
| | 0 | 44 | | { |
| | 0 | 45 | | if (!(await PhotoExists(data.Id))) |
| | 0 | 46 | | { |
| | 0 | 47 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 48 | | } |
| | 0 | 49 | | _db.Photos.Update(data); |
| | 0 | 50 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 51 | | } |
| | | 52 | | public async Task Delete(long id) |
| | 0 | 53 | | { |
| | 0 | 54 | | Photo photo = await _db.Photos |
| | 0 | 55 | | .Where(d => !d.IsDeleted) |
| | 0 | 56 | | .FirstAsync(f => f.Id == id); |
| | 0 | 57 | | if (photo == null) |
| | 0 | 58 | | throw new KeyNotFoundException($"Record #{id} not found"); |
| | 0 | 59 | | photo.IsDeleted = true; |
| | 0 | 60 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 61 | | } |
| | 0 | 62 | | public async Task<bool> PhotoExists(long id) => await _db.Photos |
| | 0 | 63 | | .Where(d => d.Id == id) |
| | 0 | 64 | | .Where(d => !d.IsDeleted) |
| | 0 | 65 | | .AnyAsync(); |
| | | 66 | | |
| | | 67 | | public List<Photo> FindPhotos(List<string> fullSizeUrls) |
| | 0 | 68 | | { |
| | 0 | 69 | | return _db.Photos.Where(x => !x.IsDeleted && fullSizeUrls.Contains(x.FullSizeUrl.ToLower())).ToList(); |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |