• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / warehouse / 22

03 Jan 2025 03:12PM UTC coverage: 93.908% (+1.9%) from 91.981%
22

push

appveyor

Sholtee
use real SQL query in GetProductDetailsById() stub, use options API where possible

1079 of 1149 relevant lines covered (93.91%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.67
/SRC/App/Warehouse.DAL/Repositories/UserRepository/UserRepository.cs
1
/********************************************************************************
2
* UserRepository.cs                                                             *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
* Project: Warehouse API (boilerplate)                                          *
6
* License: MIT                                                                  *
7
********************************************************************************/
8
using System;
9
using System.Collections.Generic;
10
using System.Data;
11
using System.Linq;
12
using System.Threading.Tasks;
13

14
using ServiceStack.OrmLite;
15

16

17
namespace Warehouse.DAL
18
{
19
    using GroupEntity = Entities.Group;
20
    using UserEntity = Entities.User;
21
    using UserGroupEntity = Entities.UserGroup;
22

23
    internal sealed class UserRepository(IDbConnection connection, IOrmLiteDialectProvider dialectProvider) : IUserRepository
1✔
24
    {
25
        public async Task<bool> CreateUser(CreateUserParam param)
26
        {
1✔
27
            Guid userId = Guid.NewGuid();
1✔
28

29
            SqlExpression<UserEntity> 
1✔
30
                userExists = connection
1✔
31
                    .From<UserEntity>()
1✔
32
                    .Select(static _ => 1)
1✔
33
                    .Where<UserEntity>(user => user.ClientId == param.ClientId),
1✔
34
                selectNewUser = connection
1✔
35
                    .From<UserEntity>(static expr => expr.FromExpression = " ")
1✔
36
                    .Select
1✔
37
                    (
1✔
38
                        _ => new
1✔
39
                        {
1✔
40
                            param.ClientId,
1✔
41
                            param.ClientSecretHash,
1✔
42
                            Id = userId,
1✔
43
                            CreatedUtc = DateTime.UtcNow 
1✔
44
                        }
1✔
45
                    )
1✔
46
                    .UnsafeWhere($"NOT EXISTS ({userExists.ToMergedParamsSelectStatement()})");
1✔
47

48
            #pragma warning disable CA2000 // false positive, Dispose() is being called on the transaction
49
            using IDbTransaction? transaction = connection.OpenTransactionIfNotExists();
1✔
50
            #pragma warning restore CA2000
51

52
            try
53
            {
1✔
54
                long rowsInserted = await connection.InsertIntoSelectAsync<UserEntity>(selectNewUser);
1✔
55
                if (rowsInserted is 0)
1✔
56
                    return false;
1✔
57

58
                SqlExpression<GroupEntity> selectNewUserGroup = connection
1✔
59
                    .From<GroupEntity>()
1✔
60
                    .Select<GroupEntity>
1✔
61
                    (
1✔
62
                        grp => new
1✔
63
                        {
1✔
64
                            GroupId = grp.Id,
1✔
65
                            UserId = userId,
1✔
66
                            Id = Sql.Custom("UUID()"),
1✔
67
                            CreatedUtc = DateTime.UtcNow
1✔
68
                        }
1✔
69
                    )
1✔
70
                    .Where<GroupEntity>(grp => Sql.In(grp.Name, param.Groups));
1✔
71

72
                rowsInserted = await connection.InsertIntoSelectAsync<UserGroupEntity>(selectNewUserGroup);
1✔
73
                if (rowsInserted != param.Groups.Count)
1✔
74
                    throw new InvalidOperationException("Invalid group");
1✔
75

76
                transaction?.Commit();
×
77
                return true;
1✔
78
            }
79
            catch
1✔
80
            {
1✔
81
                transaction?.Rollback();
×
82
                throw;
1✔
83
            }
84
        }
1✔
85

86
        public async Task<User?> QueryUser(string clientId)
87
        {
1✔
88
            string sql = connection
1✔
89
                .From<UserEntity>()
1✔
90
                .Select<UserEntity>
1✔
91
                (
1✔
92
                    user => new
1✔
93
                    {
1✔
94
                        user.ClientId,
1✔
95
                        user.ClientSecretHash,
1✔
96
                        Roles = Sql.Custom($"BIT_OR({
1✔
97
                            typeof(GroupEntity)
1✔
98
                                .GetModelMetadata()
1✔
99
                                .GetFieldDefinition<GroupEntity>(static group => group.Roles)
1✔
100
                                .GetQuotedName(dialectProvider)
1✔
101
                        })")
1✔
102
                    }
1✔
103
                )
1✔
104
                .Join<UserEntity, UserGroupEntity>(static (user, ug) => user.Id == ug.UserId)
1✔
105
                .Join<UserGroupEntity, GroupEntity>(static (ug, gr) => ug.GroupId == gr.Id)
1✔
106
                .GroupBy<UserEntity>
1✔
107
                (
1✔
108
                    static user => new
1✔
109
                    {
1✔
110
                        user.ClientId,
1✔
111
                        user.ClientSecretHash
1✔
112
                    }
1✔
113
                )
1✔
114
                .Where<UserEntity>(user => user.ClientId == clientId && user.DeletedUtc == null)
1✔
115
                .ToMergedParamsSelectStatement();
1✔
116

117
            List<User> result = await connection.SelectAsync<User>(sql);
1✔
118
            return result.SingleOrDefault();
1✔
119
        }
1✔
120

121
        public async Task<bool> DeleteUser(string clientId) => await connection.UpdateAsync<UserEntity>
1✔
122
        (
1✔
123
            new { DeletedUtc = DateTime.UtcNow },
1✔
124
            user => user.ClientId == clientId && user.DeletedUtc == null
1✔
125
        ) is 1;
1✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc