Initial commit: Lagerbestand Blazor Projekt
This commit is contained in:
commit
cba9414e92
105 files changed
+36431
No files matched your search
@@ -0,0 +1,189 @@
|
||||
using Lagerbestand.Data;
|
||||
using Lagerbestand.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Lagerbestand.Services;
|
||||
|
||||
public interface IInventoryService
|
||||
{
|
||||
Task<List<InventoryItem>> GetAllAsync();
|
||||
Task<InventoryItem?> GetByIdAsync(Guid id);
|
||||
Task<InventoryItem?> GetByQrCodeAsync(string qrCode);
|
||||
Task<bool> ExistsAsync(string qrCode);
|
||||
Task<InventoryItem> AddItemAsync(string qrCode, Guid? catalogItemId = null, string? name = null);
|
||||
Task<List<InventoryItem>> AddItemsAsync(List<string> qrCodes, Guid? catalogItemId = null);
|
||||
Task<bool> RemoveItemAsync(string qrCode);
|
||||
Task<int> RemoveItemsAsync(List<string> qrCodes);
|
||||
Task<InventoryItem> UpdateCategoryAsync(Guid id, Guid? catalogItemId);
|
||||
Task<int> UpdateCategoryForItemsAsync(List<Guid> itemIds, Guid? catalogItemId);
|
||||
Task DeleteAsync(Guid id);
|
||||
}
|
||||
|
||||
public class InventoryService : IInventoryService
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
private readonly ILogger<InventoryService> _logger;
|
||||
|
||||
public InventoryService(AppDbContext context, ILogger<InventoryService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<List<InventoryItem>> GetAllAsync()
|
||||
{
|
||||
return await _context.InventoryItems
|
||||
.AsNoTracking()
|
||||
.Include(x => x.CatalogItem)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<InventoryItem?> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await _context.InventoryItems
|
||||
.Include(x => x.CatalogItem)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
public async Task<InventoryItem?> GetByQrCodeAsync(string qrCode)
|
||||
{
|
||||
return await _context.InventoryItems
|
||||
.Include(x => x.CatalogItem)
|
||||
.FirstOrDefaultAsync(x => x.QrCode == qrCode);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(string qrCode)
|
||||
{
|
||||
return await _context.InventoryItems.AnyAsync(x => x.QrCode == qrCode);
|
||||
}
|
||||
|
||||
public async Task<InventoryItem> AddItemAsync(string qrCode, Guid? catalogItemId = null, string? name = null)
|
||||
{
|
||||
if (await _context.InventoryItems.AnyAsync(x => x.QrCode == qrCode))
|
||||
throw new InvalidOperationException($"Artikel mit QR-Code '{qrCode}' ist bereits im Bestand.");
|
||||
|
||||
var item = new InventoryItem
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
QrCode = qrCode,
|
||||
CatalogItemId = catalogItemId,
|
||||
Name = name,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.InventoryItems.Add(item);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return await GetByIdAsync(item.Id) ?? item;
|
||||
}
|
||||
|
||||
public async Task<List<InventoryItem>> AddItemsAsync(List<string> qrCodes, Guid? catalogItemId = null)
|
||||
{
|
||||
var addedItems = new List<InventoryItem>();
|
||||
var now = DateTime.UtcNow;
|
||||
var distinctCodes = qrCodes.Distinct().ToList();
|
||||
|
||||
var existingCodes = await _context.InventoryItems
|
||||
.Where(x => distinctCodes.Contains(x.QrCode))
|
||||
.Select(x => x.QrCode)
|
||||
.ToListAsync();
|
||||
var existingSet = existingCodes.ToHashSet();
|
||||
|
||||
foreach (var qrCode in distinctCodes)
|
||||
{
|
||||
if (existingSet.Contains(qrCode))
|
||||
continue;
|
||||
|
||||
var item = new InventoryItem
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
QrCode = qrCode,
|
||||
CatalogItemId = catalogItemId,
|
||||
CreatedAt = now
|
||||
};
|
||||
|
||||
_context.InventoryItems.Add(item);
|
||||
addedItems.Add(item);
|
||||
}
|
||||
|
||||
if (addedItems.Count > 0)
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return addedItems;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveItemAsync(string qrCode)
|
||||
{
|
||||
var item = await _context.InventoryItems.FirstOrDefaultAsync(x => x.QrCode == qrCode);
|
||||
if (item == null)
|
||||
return false;
|
||||
|
||||
_context.InventoryItems.Remove(item);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<int> RemoveItemsAsync(List<string> qrCodes)
|
||||
{
|
||||
var count = 0;
|
||||
foreach (var qrCode in qrCodes.Distinct())
|
||||
{
|
||||
var item = await _context.InventoryItems.FirstOrDefaultAsync(x => x.QrCode == qrCode);
|
||||
if (item != null)
|
||||
{
|
||||
_context.InventoryItems.Remove(item);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public async Task<InventoryItem> UpdateCategoryAsync(Guid id, Guid? catalogItemId)
|
||||
{
|
||||
var item = await _context.InventoryItems.FindAsync(id)
|
||||
?? throw new InvalidOperationException("Artikel nicht gefunden");
|
||||
|
||||
item.CatalogItemId = catalogItemId;
|
||||
item.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<int> UpdateCategoryForItemsAsync(List<Guid> itemIds, Guid? catalogItemId)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var count = 0;
|
||||
|
||||
foreach (var id in itemIds)
|
||||
{
|
||||
var item = await _context.InventoryItems.FindAsync(id);
|
||||
if (item != null)
|
||||
{
|
||||
item.CatalogItemId = catalogItemId;
|
||||
item.UpdatedAt = now;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
var item = await _context.InventoryItems.FindAsync(id);
|
||||
if (item != null)
|
||||
{
|
||||
_context.InventoryItems.Remove(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user