aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/DatabaseExtensions.cs
blob: a37cf05bf09bddccf625c3eaede3bb0b0391a239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
    {
        /// <summary>
        /// Check the existence and get the id of the user.
        /// </summary>
        /// <param name="username">The username of the user.</param>
        /// <returns>The user id.</returns>
        /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
        /// <exception cref="UserNotExistException">Thrown if user does not exist.</exception>
        public static async Task<long> CheckAndGetUser(DbSet<User> 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;
        }
    }
}