using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Entities;
namespace Timeline.Services
{
public static class DatabaseExtensions
{
///
/// Check the existence and get the id of the user.
///
/// The username of the user.
/// The user id.
/// Thrown if is null or empty.
/// Thrown if user does not exist.
public static async Task CheckAndGetUser(DbSet userDbSet, string username)
{
if (string.IsNullOrEmpty(username))
throw new ArgumentException("Username is null or empty.", nameof(username));
var userId = await userDbSet.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
if (userId == 0)
throw new UserNotExistException(username);
return userId;
}
}
}