From b4eb179c8f7852f8717abd949de4f5f0d5ed8f9b Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Wed, 8 Mar 2017 16:27:31 +0000 Subject: [PATCH] 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 --- .../SQLiteRepository/SQLiteRepository.cs | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index 36ef87b..d615221 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using Interfaces; using SQLite.Net; @@ -29,6 +30,8 @@ namespace SQLiteRepository _connection.CreateTable(); _connection.CreateTable(); _connection.CreateTable(); + _connection.CreateTable(); + _connection.CreateTable(); _logger.Trace("Initialised SQLite Repository"); } @@ -71,6 +74,7 @@ namespace SQLiteRepository userObj.AssociatedIdentifiers = GetAssociatedIdentifiers(user.Id); userObj.State = GetUserState(GetLogDirection(user.Id)); userObj.LastEventDateTime = GetLastLogDateTime(GetLastTimeLog(user.Id)); + userObj.Groups = GetGroups(user.Id); ret.Users.Add(userObj); } if (pageNumber == -1 && pageSize == -1) @@ -164,6 +168,12 @@ namespace SQLiteRepository var user = users.First(); 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( SQLiteProcedures.GET_CARDS_BY_USER_ID, user.Id); @@ -333,6 +343,12 @@ namespace SQLiteRepository } #endregion + #region Update Group Associations + + SetUserGroups(user.UserId, user.Groups.Where(x=>x.IsAssociatedToUser).ToList()); + + #endregion + userIdResult = userId; return ret; } @@ -410,6 +426,93 @@ namespace SQLiteRepository 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 GetGroups(int userId=-1) + { + var ret = new List(); + List query; + if (userId == -1) + { + query = _connection.Query("select * from GroupDb"); + } + else + { + var t = new UserGroupJoinDb(); + query = + _connection.Query( + "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(groupId); + return OperationResponse.DELETED; + } + + private bool SetUserGroups(int userId, List 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("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 groupNames) + { + var ret = new List(); + + foreach (var g in groupNames) + { + var query = _connection.Query("select GroupId from GroupDb where GroupName=?", g); + if (!query.Any()) continue; + var id = query.First(); + ret.Add(id.GroupId); + } + return ret.ToArray(); + } + private List GetTimeLogList(int userId, int calendarWeek, int year) { var timeLogList = _connection.Query(