Initial commit: Lagerbestand Blazor Projekt
This commit is contained in:
commit
cba9414e92
105 files changed
+36431
No files matched your search
@@ -0,0 +1,114 @@
|
||||
using Lagerbestand.Data;
|
||||
using Lagerbestand.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Lagerbestand.Services;
|
||||
|
||||
public interface ICatalogService
|
||||
{
|
||||
Task<List<CatalogItem>> GetAllAsync();
|
||||
Task<CatalogItem?> GetByIdAsync(Guid id);
|
||||
Task<CatalogItem> CreateAsync(CatalogItem item);
|
||||
Task<CatalogItem> UpdateAsync(CatalogItem item);
|
||||
Task DeleteAsync(Guid id);
|
||||
Task<string> SaveImageAsync(Stream imageStream, string fileName);
|
||||
Task DeleteImageAsync(string? fileName);
|
||||
}
|
||||
|
||||
public class CatalogService : ICatalogService
|
||||
{
|
||||
private static readonly string[] AllowedImageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp"];
|
||||
|
||||
private readonly AppDbContext _context;
|
||||
private readonly ILogger<CatalogService> _logger;
|
||||
private readonly string _uploadPath;
|
||||
|
||||
public CatalogService(AppDbContext context, IWebHostEnvironment environment, ILogger<CatalogService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_uploadPath = Path.Combine(environment.WebRootPath, "uploads", "images");
|
||||
|
||||
Directory.CreateDirectory(_uploadPath);
|
||||
}
|
||||
|
||||
public async Task<List<CatalogItem>> GetAllAsync()
|
||||
{
|
||||
return await _context.CatalogItems
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<CatalogItem?> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await _context.CatalogItems.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<CatalogItem> CreateAsync(CatalogItem item)
|
||||
{
|
||||
item.Id = Guid.NewGuid();
|
||||
item.CreatedAt = DateTime.UtcNow;
|
||||
|
||||
_context.CatalogItems.Add(item);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<CatalogItem> UpdateAsync(CatalogItem item)
|
||||
{
|
||||
var existing = await _context.CatalogItems.FindAsync(item.Id)
|
||||
?? throw new InvalidOperationException("Artikel nicht gefunden");
|
||||
|
||||
existing.Name = item.Name;
|
||||
existing.TireSize = item.TireSize;
|
||||
existing.Season = item.Season;
|
||||
existing.Type = item.Type;
|
||||
existing.PictureFileName = item.PictureFileName;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
var item = await _context.CatalogItems.FindAsync(id);
|
||||
if (item != null)
|
||||
{
|
||||
await DeleteImageAsync(item.PictureFileName);
|
||||
_context.CatalogItems.Remove(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> SaveImageAsync(Stream imageStream, string fileName)
|
||||
{
|
||||
var extension = Path.GetExtension(fileName).ToLowerInvariant();
|
||||
|
||||
if (!AllowedImageExtensions.Contains(extension))
|
||||
throw new InvalidOperationException("Ungültiger Dateityp. Erlaubt: jpg, jpeg, png, gif, webp");
|
||||
|
||||
var uniqueFileName = $"{Guid.NewGuid()}{extension}";
|
||||
var filePath = Path.Combine(_uploadPath, uniqueFileName);
|
||||
|
||||
await using var fileStream = new FileStream(filePath, FileMode.Create);
|
||||
await imageStream.CopyToAsync(fileStream);
|
||||
|
||||
return uniqueFileName;
|
||||
}
|
||||
|
||||
public Task DeleteImageAsync(string? fileName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileName))
|
||||
return Task.CompletedTask;
|
||||
|
||||
var filePath = Path.Combine(_uploadPath, fileName);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user