-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathPersonnelLocationsDocRepository.cs
More file actions
158 lines (135 loc) · 5.04 KB
/
Copy pathPersonnelLocationsDocRepository.cs
File metadata and controls
158 lines (135 loc) · 5.04 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Dapper;
using Newtonsoft.Json;
using Npgsql;
using Resgrid.Model;
using Resgrid.Model.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Resgrid.Repositories.NoSqlRepository
{
public class PersonnelLocationsDocRepository : IPersonnelLocationsDocRepository
{
public async Task<List<PersonnelLocation>> GetAllLocationsByUnitIdAsync(string userId)
{
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var personLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.userid = @userId ORDER BY timestamp DESC;",
new { userId });
if (personLocationsData != null)
return personLocationsData.ToList();
else
return new List<PersonnelLocation>();
}
}
public async Task<PersonnelLocation> GetLatestLocationsByUnitIdAsync(string userId)
{
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.userid = @userId ORDER BY timestamp DESC LIMIT 1;",
new { userId });
if (unitLocationsData != null)
return unitLocationsData.FirstOrDefault();
else
return null;
}
}
public async Task<List<PersonnelLocation>> GetLatestLocationsByDepartmentIdAsync(int departmentId)
{
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT DISTINCT ON (userid) data FROM public.personnellocations ul WHERE ul.departmentid = @departmentId ORDER BY ul.userid, ul.timestamp DESC;",
new { departmentId });
if (unitLocationsData != null)
return unitLocationsData.ToList();
else
return new List<PersonnelLocation>();
}
}
public async Task<PersonnelLocation> GetByIdAsync(string id)
{
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.oid = @id;",
new { id });
if (unitLocationsData != null && unitLocationsData.Any())
return unitLocationsData.FirstOrDefault();
if (!int.TryParse(id, out var numericId))
return null;
var unitLocationsData2 = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.id = @id;",
new { id = numericId });
if (unitLocationsData2 != null && unitLocationsData2.Any())
return unitLocationsData2.FirstOrDefault();
else
return null;
}
}
public async Task<PersonnelLocation> GetByOldIdAsync(string id)
{
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var personnelLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.oid = @id;",
new { id });
if (personnelLocationsData != null)
return personnelLocationsData.FirstOrDefault();
else
return null;
}
}
public async Task<PersonnelLocation> InsertAsync(PersonnelLocation location)
{
var dataJson = JsonConvert.SerializeObject(location);
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var result = await connection.ExecuteScalarAsync<string>(
"INSERT INTO public.personnellocations (departmentid, userid, data) VALUES (@departmentId, @userId, CAST(@dataJson AS jsonb)) RETURNING id::text;",
new
{
departmentId = location.DepartmentId,
userId = location.UserId,
dataJson
});
location.PgId = result;
return location;
}
}
public async Task<PersonnelLocation> UpdateAsync(PersonnelLocation location)
{
if (location == null)
throw new ArgumentNullException(nameof(location));
if (string.IsNullOrWhiteSpace(location.PgId))
throw new InvalidOperationException("Personnel location PgId is required for updates.");
if (!int.TryParse(location.PgId, out var pgId))
throw new ArgumentException("Personnel location PgId must be a valid integer.", nameof(location));
var dataJson = JsonConvert.SerializeObject(location);
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
await connection.ExecuteAsync(
"UPDATE public.personnellocations SET departmentid = @departmentId, userid = @userId, data = CAST(@dataJson AS jsonb) WHERE id = @id;",
new
{
departmentId = location.DepartmentId,
userId = location.UserId,
dataJson,
id = pgId
});
return location;
}
}
}
}