Use transactions
This commit is contained in:
@@ -62,23 +62,21 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Category> AddAsync(Category category, CancellationToken cancellationToken = default)
|
||||
public Task<Category> AddAsync(Category category, CancellationToken cancellationToken = default)
|
||||
{
|
||||
category.Id = Guid.NewGuid();
|
||||
category.CreatedAt = DateTime.UtcNow;
|
||||
category.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
context.Categories.Add(category);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return category;
|
||||
return Task.FromResult(category);
|
||||
}
|
||||
|
||||
public async Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
|
||||
public Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
|
||||
{
|
||||
category.ModifiedAt = DateTime.UtcNow;
|
||||
context.Categories.Update(category);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return category;
|
||||
return Task.FromResult(category);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
@@ -87,7 +85,6 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
|
||||
if (category != null)
|
||||
{
|
||||
context.Categories.Remove(category);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Imprink.Domain.Entities.Product;
|
||||
using Imprink.Domain.Models;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Domain.Repositories.Products;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -143,12 +142,11 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
|
||||
return product;
|
||||
}
|
||||
|
||||
public async Task<Product> UpdateAsync(Product product, CancellationToken cancellationToken = default)
|
||||
public Task<Product> UpdateAsync(Product product, CancellationToken cancellationToken = default)
|
||||
{
|
||||
product.ModifiedAt = DateTime.UtcNow;
|
||||
context.Products.Update(product);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return product;
|
||||
return Task.FromResult(product);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
@@ -157,7 +155,6 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
|
||||
if (product != null)
|
||||
{
|
||||
context.Products.Remove(product);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,23 +58,21 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<ProductVariant> AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
public Task<ProductVariant> AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
productVariant.Id = Guid.NewGuid();
|
||||
productVariant.CreatedAt = DateTime.UtcNow;
|
||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
context.ProductVariants.Add(productVariant);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return productVariant;
|
||||
return Task.FromResult(productVariant);
|
||||
}
|
||||
|
||||
public async Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
public Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||
context.ProductVariants.Update(productVariant);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return productVariant;
|
||||
return Task.FromResult(productVariant);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
@@ -83,7 +81,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
||||
if (productVariant != null)
|
||||
{
|
||||
context.ProductVariants.Remove(productVariant);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +109,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
||||
{
|
||||
productVariant.StockQuantity = quantity;
|
||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Domain.Repositories.Users;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -33,17 +32,15 @@ public class UserRoleRepository(ApplicationDbContext context) : IUserRoleReposit
|
||||
.FirstOrDefaultAsync(ur => ur.UserId == userId && ur.RoleId == roleId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<UserRole> AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
public Task<UserRole> AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ur = context.UserRole.Add(userRole);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return ur.Entity;
|
||||
return Task.FromResult(ur.Entity);
|
||||
}
|
||||
|
||||
public async Task<UserRole> RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
public Task<UserRole> RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ur = context.UserRole.Remove(userRole);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return ur.Entity;
|
||||
return Task.FromResult(ur.Entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user