You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.9 KiB
98 lines
2.9 KiB
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbImagesManager : IImagesService<Images>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Images> _repository;
|
|
|
|
public DbImagesManager(WTFContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repository = new GenericRepository<Images>(context);
|
|
}
|
|
|
|
public async Task AddImage(Images image)
|
|
{
|
|
_repository.Insert(image);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<PaginationResult<Images>> GetAllImage()
|
|
{
|
|
return new PaginationResult<Images>(await CountImage(), 0, await CountImage(), _repository.GetItems(0, await CountImage()).ToList());
|
|
}
|
|
|
|
public async Task<Images> GetImageById(int id)
|
|
{
|
|
return _repository.GetById(id);
|
|
}
|
|
|
|
public async Task<int> GetLastImageId()
|
|
{
|
|
var last = await GetAllImage();
|
|
int id = 1;
|
|
{
|
|
foreach (Images image in last.items)
|
|
{
|
|
if(image.Id >= id)
|
|
id = image.Id + 1;
|
|
}
|
|
}
|
|
return id;
|
|
}
|
|
|
|
public async Task<PaginationResult<Images>> GetSomeImage(int index, int pageSize)
|
|
{
|
|
var images = _repository.GetItems(index, pageSize);
|
|
return new PaginationResult<Images>(images.Count(),index,pageSize,images.ToList());
|
|
}
|
|
|
|
public async Task RemoveImage(int id)
|
|
{
|
|
_repository.Delete(id);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateImage(int id, Images image)
|
|
{
|
|
var img = _repository.GetById(id);
|
|
var modif = false;
|
|
if (image != null && img != null)
|
|
{
|
|
if (image.ImgPath != null)
|
|
{
|
|
img.ImgPath = image.ImgPath;
|
|
modif = true;
|
|
}
|
|
_repository.Update(img);
|
|
if (modif)
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<int> CountImage()
|
|
{
|
|
return await _context.images.CountAsync();
|
|
}
|
|
|
|
public async Task<Images?> GetImageByPath(string path)
|
|
{
|
|
var image = _repository.GetItems(item => item.ImgPath == path,0,1).FirstOrDefault();
|
|
return image;
|
|
}
|
|
}
|
|
}
|