-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserProfileService.cs
117 lines (99 loc) · 4.1 KB
/
UserProfileService.cs
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
using Sabio.Data;
using Sabio.Data.Providers;
using Sabio.Models.Domain;
using Sabio.Models.Requests.UserProfile;
using Sabio.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sabio.Services
{
public class UserProfileService : IUserProfileService
{
IDataProvider _data = null;
public UserProfileService(IDataProvider data)
{
this._data = data;
}
public int Add(UserProfileAddRequest model, int userId)
{
int id = 0;
string procName = "[dbo].[UserProfile_Insert_V2]";
_data.ExecuteNonQuery(procName, inputParamMapper: delegate (SqlParameterCollection col)
{
col.AddWithValue("@GivenName", model.GivenName);
col.AddWithValue("@Surnames", model.Surnames);
col.AddWithValue("@AvatarUrl", model.AvatarUrl);
col.AddWithValue("@UserId", userId);
col.AddWithValue("@Url", model.Url);
col.AddWithValue("@UrlTypeId", model.UrlTypeId);
SqlParameter idOut = new SqlParameter("@ProfileId", SqlDbType.Int);
idOut.Direction = ParameterDirection.Output;
col.Add(idOut);
}, returnParameters: delegate (SqlParameterCollection returnCollection)
{
object oId = returnCollection["@ProfileId"].Value;
int.TryParse(oId.ToString(), out id);
});
return id;
}
public void Update(UserProfileUpdateRequest model)
{
string procName = "[dbo].[UserProfile_Update_V2]";
_data.ExecuteNonQuery(procName, inputParamMapper: delegate (SqlParameterCollection col)
{
col.AddWithValue("@GivenName", model.GivenName);
col.AddWithValue("@Surnames", model.Surnames);
col.AddWithValue("@AvatarUrl", model.AvatarUrl);
col.AddWithValue("@Url", model.Url);
col.AddWithValue("@ProfileId", model.Id);
});
}
public UserProfile GetByUserId(int id)
{
string procName = "[dbo].[UserProfile_GetByUserId]";
UserProfile userProfile = null;
List<LookUp<int>> list = null;
_data.ExecuteCmd(procName, delegate (SqlParameterCollection col)
{
col.AddWithValue("@UserId", id);
}, delegate (IDataReader reader, short set)
{
userProfile = new UserProfile();
int startingIndex = 0;
list = new List<LookUp<int>>();
userProfile.ProfileId = reader.GetSafeInt32(startingIndex++);
userProfile.GivenName = reader.GetSafeString(startingIndex++);
userProfile.Surnames = reader.GetSafeString(startingIndex++);
userProfile.AvatarUrl = reader.GetSafeString(startingIndex++);
userProfile.UserId = reader.GetSafeInt32(startingIndex++);
userProfile.Email = reader.GetSafeString(startingIndex++);
list = reader.DeserializeObject<List<LookUp<int>>>(startingIndex++);
if (list != null)
{
userProfile.Roles = MapRoles(list);
}
userProfile.DateCreated = reader.GetSafeDateTime(startingIndex++);
userProfile.DateModified = reader.GetSafeDateTime(startingIndex++);
});
return userProfile;
}
private List<string> MapRoles(List<LookUp<int>> list)
{
List<string> newList = null;
foreach (LookUp<int> item in list)
{
if (newList == null)
{
newList = new List<string>();
}
newList.Add(item.Name);
}
return newList;
}
}
}