Spaces:
Sleeping
Sleeping
Download src/ClipForge.Infrastructure/Repositories/BaseRepository.cs from clipeforge/app: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Repositories/BaseRepository.cs
- Command line
-
hf download hf://spaces/clipeforge/app/src/ClipForge.Infrastructure/Repositories/BaseRepository.cs
-
curl -L -o BaseRepository.cs https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Repositories/BaseRepository.cs
1.4 kB
| using ClipForge.Core.Entities; | |
| using ClipForge.Core.Interfaces; | |
| using ClipForge.Infrastructure.Data; | |
| using Microsoft.EntityFrameworkCore; | |
| namespace ClipForge.Infrastructure.Repositories; | |
| public class BaseRepository<T> : IRepository<T> where T : BaseEntity | |
| { | |
| protected readonly AppDbContext _context; | |
| protected readonly DbSet<T> _dbSet; | |
| public BaseRepository(AppDbContext context) | |
| { | |
| _context = context; | |
| _dbSet = context.Set<T>(); | |
| } | |
| public async Task<T?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| => await _dbSet.FindAsync([id], ct); | |
| public async Task<IReadOnlyList<T>> GetAllAsync(CancellationToken ct = default) | |
| => await _dbSet.OrderByDescending(e => e.CreatedAt).ToListAsync(ct); | |
| public async Task<T> AddAsync(T entity, CancellationToken ct = default) | |
| { | |
| await _dbSet.AddAsync(entity, ct); | |
| await _context.SaveChangesAsync(ct); | |
| return entity; | |
| } | |
| public async Task UpdateAsync(T entity, CancellationToken ct = default) | |
| { | |
| _dbSet.Update(entity); | |
| await _context.SaveChangesAsync(ct); | |
| } | |
| public async Task DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var entity = await GetByIdAsync(id, ct); | |
| if (entity is not null) | |
| { | |
| _dbSet.Remove(entity); | |
| await _context.SaveChangesAsync(ct); | |
| } | |
| } | |
| } | |