using JobsJobsJobs.Shared;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace JobsJobsJobs.Server.Data
{
///
/// Extensions to JobsDbContext supporting the manipulation of citizens
///
public static class CitizenExtensions
{
///
/// Retrieve a citizen by their Jobs, Jobs, Jobs ID
///
/// The ID of the citizen to retrieve
/// The citizen, or null if not found
public static async Task FindCitizenById(this JobsDbContext db, CitizenId citizenId) =>
await db.Citizens.AsNoTracking()
.SingleOrDefaultAsync(c => c.Id == citizenId)
.ConfigureAwait(false);
///
/// Retrieve a citizen by their No Agenda Social user name
///
/// The NAS user name
/// The citizen, or null if not found
public static async Task FindCitizenByNAUser(this JobsDbContext db, string naUser) =>
await db.Citizens.AsNoTracking()
.SingleOrDefaultAsync(c => c.NaUser == naUser)
.ConfigureAwait(false);
///
/// Add a citizen
///
/// The citizen to be added
public static async Task AddCitizen(this JobsDbContext db, Citizen citizen) =>
await db.Citizens.AddAsync(citizen);
///
/// Update a citizen after they have logged on (update last seen, sync display name)
///
/// The updated citizen
public static void UpdateCitizen(this JobsDbContext db, Citizen citizen) =>
db.Entry(citizen).State = EntityState.Modified;
}
}