-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamService.cs
136 lines (115 loc) · 5.06 KB
/
TeamService.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Messenger.Core.Helpers;
using Messenger.Core.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
namespace Messenger.Core.Services
{
public class TeamService : AzureServiceBase
{
#region Teams Management
/// <summary>
/// Creates a team with the given name and description
/// </summary>
/// <param name="teamName">Name of the team</param>
/// <param name="teamDescription">Description of the team</param>
/// <returns>True if no exceptions occured while executing the query, false otherwise</returns>
public async Task<bool> CreateTeam(string teamName, string teamDescription = "")
{
if (teamName == string.Empty) return false;
string query = $"INSERT INTO Teams (TeamName, TeamDescription, CreationDate) VALUES " +
$"('{teamName}', '{teamDescription}', GETDATE());";
return await SqlHelpers.NonQueryAsync(query, GetConnection());
}
/// <summary>
/// Deletes a team with a given team id.
/// </summary>
/// <param name="teamId">The id of the team to delete</param>
/// <returns>True if no exceptions occured while executing the query, false otherwise</returns>
public async Task<bool> DeleteTeam(int teamId)
{
string query = $"DELETE FROM Teams WHERE TeamId={teamId};";
return await SqlHelpers.NonQueryAsync(query, GetConnection());
}
/// <summary>
/// Returns a list of teams.
/// </summary>
/// <returns>An enumerable of Team objects</returns>
public async Task<IEnumerable<Team>> GetAllTeams()
{
string query = @"SELECT TeamId, TeamName, TeamDescription, CreationDate FROM Teams;";
try
{
using (SqlConnection connection = GetConnection())
{
await connection.OpenAsync();
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Teams");
return dataSet.Tables["Teams"].Rows.Cast<DataRow>().Select(Mapper.TeamFromDataRow);
}
}
catch (Exception e)
{
Debug.WriteLine($"Database Exception: {e.Message}/{e.InnerException?.Message}");
return null;
}
}
#endregion
#region Members Management
/// <summary>
/// Adds a new member to the team
/// </summary>
/// <param name="userId">The id of the user to add to the specified team</param>
/// <param name="teamId">The id of the team to add the specified user to</param>
/// <returns>True if no exceptions occured while executing the query, false otherwise</returns>
public async Task<bool> AddMember(string userId, int teamId)
{
string query = $"INSERT INTO Memberships(UserId, TeamId, UserRole) VALUES('{userId}', {teamId}, 'placeholder');";
return await SqlHelpers.NonQueryAsync(query, GetConnection());
}
/// <summary>
/// Removes a member from the team
/// </summary>
/// <param name="userId">The id of the user to remove from the specified team</param>
/// <param name="teamId">The id of the team to remove the specified user from</param>
/// <returns>True if no exceptions occured while executing the query, false otherwise</returns>
public async Task<bool> RemoveMember(string userId, int teamId)
{
string query = $"DELETE FROM Memberships WHERE UserId='{userId}' AND TeamId={teamId};";
return await SqlHelpers.NonQueryAsync(query, GetConnection());
}
/// <summary>
/// Gets all members in the team
/// </summary>
/// <param name="teamId">The id of the team to get members of</param>
/// <returns>An enumerable of User objects</returns>
public async Task<IEnumerable<User>> GetAllMembers(int teamId)
{
string subquery = $"SELECT UserId FROM Memberships WHERE TeamId={teamId}";
string query = $"SELECT * FROM Users WHERE UserId IN ({subquery})";
try
{
using (SqlConnection connection = GetConnection())
{
await connection.OpenAsync();
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Users");
return dataSet.Tables["Users"].Rows.Cast<DataRow>().Select(Mapper.UserFromDataRow);
}
}
catch (Exception e)
{
Debug.WriteLine($"Database Exception: {e.Message}/{e.InnerException?.Message}");
return null;
}
}
#endregion
}
}