NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using BookShop.Application.Asbtarcts.Common;
using BookShop.Application.Asbtarcts.Repository.Base;
using BookShop.Domain.Base;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Drawing;
using System.Linq.Expressions;

namespace BookShop.Application.Contracts.Repository.Base;

public class Repository<TDbContext, TEntity, TKey> : IRepository<TDbContext, TEntity, TKey>
where TEntity : BaseEntity<TKey>
where TDbContext : IApplicationDbContext
where TKey : IEquatable<TKey>
{
private readonly TDbContext _context;

public Repository(TDbContext context)
{
_context = context;
}

public DbSet<TEntity> Table
{
get
{
DbSet<TEntity> table = Table;
return table;
}
}

public async Task<TEntity?> GetAsync(TKey id)
=> await Table.FindAsync(id);

public async Task<TEntity?> GetAsync(
Expression<Func<TEntity, bool>>? predicate = null,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? await query.FirstOrDefaultAsync()
: await query.Where(predicate).FirstOrDefaultAsync();
}

public async Task<TResult?> GetWithSelectAsync<TResult>(
Expression<Func<TEntity, TResult>> select,
Expression<Func<TEntity, bool>>? predicate = null,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? await query.Select(select).FirstOrDefaultAsync()
: await query.Where(predicate).Select(select).FirstOrDefaultAsync();
}

public async Task<IEnumerable<TEntity?>> GetAllAsync(
Expression<Func<TEntity, bool>>? predicate = null,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? await query.ToListAsync()
: await query.Where(predicate).ToListAsync();
}

public async Task<List<TEntity>> GetAllAsync<TOrderBy>(
int page,
int size,
Expression<Func<TEntity, TOrderBy>> orderBy,
Expression<Func<TEntity, bool>>? predicate = null,
bool isOrderBy = true,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? isOrderBy
? await query.Skip((page - 1) * size).Take(size).ToListAsync()
: await query.OrderByDescending(orderBy).Skip((page - 1) * size).Take(size).ToListAsync()
: isOrderBy
? await query.Where(predicate).Skip((page - 1) * size).Take(size).ToListAsync()
: await query.Where(predicate).OrderByDescending(orderBy).Skip((page - 1) * size).Take(size).ToListAsync();
}

public async Task<List<TResult>> GetAllWithSelectAsync<TOrderBy, TResult>(
Expression<Func<TEntity, TOrderBy>> orderBy,
Expression<Func<TEntity, TResult>> select,
Expression<Func<TEntity, bool>>? predicate = null,
bool isOrderBy = true,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? isOrderBy
? await query.Select(select).ToListAsync()
: await query.OrderByDescending(orderBy).Select(select).ToListAsync()
: isOrderBy
? await query.Where(predicate).Select(select).ToListAsync()
: await query.Where(predicate).OrderByDescending(orderBy).Select(select).ToListAsync();
}

public async Task<List<TResult>> GetAllWithSelectAsync<TOrderBy, TResult>(
int page,
int size,
Expression<Func<TEntity, TOrderBy>> orderBy,
Expression<Func<TEntity, TResult>> select,
Expression<Func<TEntity, bool>>? predicate = null,
bool isOrderBy = true,
bool tracking = true,
params string[] includes)
{
IQueryable<TEntity> query = GetQuery(includes);
if (!tracking)
query = Table.AsNoTracking();
return predicate is null
? isOrderBy
? await query.Skip((page - 1) * size).Take(size).Select(select).ToListAsync()
: await query.OrderByDescending(orderBy).Skip((page - 1) * size).Take(size).Select(select).ToListAsync()
: isOrderBy
? await query.Where(predicate).Skip((page - 1) * size).Take(size).Select(select).ToListAsync()
: await query.Where(predicate).OrderByDescending(orderBy).Skip((page - 1) * size).Take(size).Select(select).ToListAsync();
}

public async Task AddAsync(TEntity entity)
{
EntityEntry newEntity = await Table.AddAsync(entity);
newEntity.State = EntityState.Added;
}
public void Remove(TEntity entity)
{
EntityEntry newEntity = Table.Remove(entity);
newEntity.State = EntityState.Deleted;
}


public void Update(TEntity entity)
{
EntityEntry newEntity = Table.Update(entity);
newEntity.State = EntityState.Modified;
}

public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await _context.SaveChangesAsync(cancellationToken);
}

private IQueryable<TEntity> GetQuery(params string[] includes)
{
IQueryable<TEntity> query = Table.AsQueryable();
if (includes != null)
{
foreach (string item in includes)
{
query = query.Include(item);
}
}
return query;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.