mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-05 20:40:55 +00:00
51 lines
2.6 KiB
C#
51 lines
2.6 KiB
C#
using CartriX.Api.Classes;
|
|
using CartriX.Api.Entities;
|
|
using CartriX.Api.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CartriX.Api.Services
|
|
{
|
|
public class CartridgeService : ICartridgeService
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public CartridgeService(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
public async Task<Entities.Cartridge?> GetAsync(int id) => await _context.Cartridges.Include(c => c.model).ThenInclude(m => m.manufacturer).Include(c => c.model).ThenInclude(m => m.articles).Include(c => c.status).FirstAsync(c => c.id == id);
|
|
public async Task<List<Entities.Cartridge>> GetListAsync() => await _context.Cartridges.Include(c => c.model).ThenInclude(m => m.articles).Include(c => c.status).ToListAsync();
|
|
public async Task<List<string>> GetModelsListAsync() => await _context.CartridgeModels.Select(m => m.name).ToListAsync();
|
|
public async Task<List<Entities.CartridgeModel>> GetModelsWithManufacturersListAsync() => await _context.CartridgeModels.Include(m => m.manufacturer).ToListAsync();
|
|
public async Task<List<CartridgeModel>> GetModelNameAndManufacturerAsync() => await _context.CartridgeModels.Include(c => c.manufacturer).ToListAsync();
|
|
public async Task AddAsync(DTOs.Cartridge.Add dataCartridge)
|
|
{
|
|
var manufacturer = (dataCartridge.manufacturerName is not null) ? await _context.Manufacturers.FirstOrDefaultAsync(m => m.name == dataCartridge.manufacturerName) ?? new() { name = dataCartridge.manufacturerName } : null;
|
|
var articles = new List<Entities.Article>();
|
|
|
|
foreach (string article in dataCartridge.articles)
|
|
{
|
|
articles.Add(_context.Articles.FirstOrDefault(a => a.name == article.ToUpper()) ?? new() { name = article.ToUpper() });
|
|
}
|
|
var model = await _context.CartridgeModels.FirstOrDefaultAsync(cm => cm.name == dataCartridge.modelName.ToUpper()) ?? new() { name = dataCartridge.modelName.ToUpper(), manufacturer = manufacturer, articles = articles };
|
|
|
|
await _context.Cartridges.AddAsync(new Cartridge() { model = model });
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id)
|
|
{
|
|
Cartridge? findCartridge = await _context.Cartridges.FindAsync(id);
|
|
if (findCartridge is null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
_context.Cartridges.Remove(findCartridge);
|
|
_context.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
} |