aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/User/BasicUserService.cs
blob: 1f1b25f55e48f52a4827618de7e830c9bb6ae1af (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Models.Validation;

namespace Timeline.Services.User
{
    public class BasicUserService : IBasicUserService
    {
        private readonly DatabaseContext _database;

        private readonly UsernameValidator _usernameValidator = new UsernameValidator();

        public BasicUserService(DatabaseContext database)
        {
            _database = database;
        }

        public async Task<bool> CheckUserExistenceAsync(long id)
        {
            return await _database.Users.AnyAsync(u => u.Id == id);
        }

        public async Task<long> GetUserIdByUsernameAsync(string username)
        {
            if (username == null)
                throw new ArgumentNullException(nameof(username));

            if (!_usernameValidator.Validate(username, out var message))
                throw new ArgumentException(message);

            var entity = await _database.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync();

            if (entity == null)
                throw new UserNotExistException(username);

            return entity.Id;
        }

        public async Task<DateTime> GetUsernameLastModifiedTimeAsync(long userId)
        {
            var entity = await _database.Users.Where(u => u.Id == userId).Select(u => new { u.UsernameChangeTime }).SingleOrDefaultAsync();

            if (entity is null)
                throw new UserNotExistException(userId);

            return entity.UsernameChangeTime;
        }
    }

    public static class BasicUserServiceExtensions
    {
        public static async Task ThrowIfUserNotExist(this IBasicUserService service, long userId)
        {
            if (!await service.CheckUserExistenceAsync(userId))
            {
                throw new UserNotExistException(userId);
            }
        }
    }
}