implemented the CreateGroup, GetGroups, UpdateGroup, DeleteGroup methods.

updated GetUsers and GetUser to populate the User object with groups list.
modified UpdateUser to link the User to the Group items in the DB
#59
This commit is contained in:
Chris.Watts90@outlook.com 2017-03-08 16:27:31 +00:00
parent a047c20aea
commit b4eb179c8f

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Reflection; using System.Reflection;
using Interfaces; using Interfaces;
using SQLite.Net; using SQLite.Net;
@ -29,6 +30,8 @@ namespace SQLiteRepository
_connection.CreateTable<CardUniqueId>(); _connection.CreateTable<CardUniqueId>();
_connection.CreateTable<UserIdentity>(); _connection.CreateTable<UserIdentity>();
_connection.CreateTable<TimeLogDb>(); _connection.CreateTable<TimeLogDb>();
_connection.CreateTable<GroupDb>();
_connection.CreateTable<UserGroupJoinDb>();
_logger.Trace("Initialised SQLite Repository"); _logger.Trace("Initialised SQLite Repository");
} }
@ -71,6 +74,7 @@ namespace SQLiteRepository
userObj.AssociatedIdentifiers = GetAssociatedIdentifiers(user.Id); userObj.AssociatedIdentifiers = GetAssociatedIdentifiers(user.Id);
userObj.State = GetUserState(GetLogDirection(user.Id)); userObj.State = GetUserState(GetLogDirection(user.Id));
userObj.LastEventDateTime = GetLastLogDateTime(GetLastTimeLog(user.Id)); userObj.LastEventDateTime = GetLastLogDateTime(GetLastTimeLog(user.Id));
userObj.Groups = GetGroups(user.Id);
ret.Users.Add(userObj); ret.Users.Add(userObj);
} }
if (pageNumber == -1 && pageSize == -1) if (pageNumber == -1 && pageSize == -1)
@ -164,6 +168,12 @@ namespace SQLiteRepository
var user = users.First(); var user = users.First();
ret = ChangeToUserObject(user); ret = ChangeToUserObject(user);
ret.Groups = GetGroups();
var usersGroups = GetGroups(user.Id);
foreach (var group in usersGroups)
{
ret.Groups.First(x => x.Id == group.Id).IsAssociatedToUser = true;
}
var cards = _connection.Query<CardUniqueId>( var cards = _connection.Query<CardUniqueId>(
SQLiteProcedures.GET_CARDS_BY_USER_ID, SQLiteProcedures.GET_CARDS_BY_USER_ID,
user.Id); user.Id);
@ -333,6 +343,12 @@ namespace SQLiteRepository
} }
#endregion #endregion
#region Update Group Associations
SetUserGroups(user.UserId, user.Groups.Where(x=>x.IsAssociatedToUser).ToList());
#endregion
userIdResult = userId; userIdResult = userId;
return ret; return ret;
} }
@ -410,6 +426,93 @@ namespace SQLiteRepository
return OperationResponse.SUCCESS; return OperationResponse.SUCCESS;
} }
/*Groups*/
//TODO: check group name can only be entered once.
public OperationResponse CreateGroup(Group group, out int groupId)
{
var groupDb = new GroupDb { GroupName=group.Name};
var resp = _connection.Insert(groupDb);
groupId = groupDb.GroupId;
return OperationResponse.CREATED;
}
public List<Group> GetGroups(int userId=-1)
{
var ret = new List<Group>();
List<GroupDb> query;
if (userId == -1)
{
query = _connection.Query<GroupDb>("select * from GroupDb");
}
else
{
var t = new UserGroupJoinDb();
query =
_connection.Query<GroupDb>(
"select gdb.GroupId, gdb.GroupName, gdb.AssignedUserCount"+
" from GroupDb gdb"+
" left join UserGroupJoinDb ujdb"+
" on gdb.GroupId = ujdb.GroupId_FK"+
//"left join UserIdentity ui"+
//"on ujdb.UserId_FK = ui.Id"+
" where ujdb.UserId_FK = 1",
//"select * from GroupDb gdb left join UserGroupJoinDb ujdb on gdb.GroupId=ujdb.GroupId_FK where ujdb.UserId_FK = ?",
userId);
}
foreach (var group in query)
{
ret.Add(new Group
{
Id=group.GroupId,
Name = group.GroupName,
UserCount = 0
});
}
return ret;
}
public OperationResponse UpdateGroup(Group group)
{
//TODO: I would probably prefer to do this manually....
var resp = _connection.Update(group);
return OperationResponse.UPDATED;
}
public OperationResponse DeleteGroup(int groupId)
{
_connection.Delete<GroupDb>(groupId);
return OperationResponse.DELETED;
}
private bool SetUserGroups(int userId, List<Group> groups)
{
var groupIds = GetGroupIds(groups.Select(x=>x.Name).ToList());
return SetUserGroups(userId, groupIds);
}
private bool SetUserGroups(int userId, int[] groupIds)
{
//remove the existing user>group associations
_connection.Query<UserGroupJoinDb>("delete from UserGroupJoinDb where UserId_FK = ?", userId);
//add the new group associations.
_connection.InsertAll(groupIds.Select(x => new UserGroupJoinDb {GroupId_FK = x, UserId_FK = userId}));
return true;
}
private int[] GetGroupIds(List<string> groupNames)
{
var ret = new List<int>();
foreach (var g in groupNames)
{
var query = _connection.Query<GroupDb>("select GroupId from GroupDb where GroupName=?", g);
if (!query.Any()) continue;
var id = query.First();
ret.Add(id.GroupId);
}
return ret.ToArray();
}
private List<DailyLogs> GetTimeLogList(int userId, int calendarWeek, int year) private List<DailyLogs> GetTimeLogList(int userId, int calendarWeek, int year)
{ {
var timeLogList = _connection.Query<TimeLogDb>( var timeLogList = _connection.Query<TimeLogDb>(