/// <summary> /// 使用清單名稱、帳號,新增清單 /// </summary> /// <param name="listName">清單名稱</param> /// <param name="account">帳號</param> /// <returns>新增結果</returns> public PoResult <ToDoList> CreateList(string listName, string account) { try { var user = this.Database.Users.FirstOrDefault(o => o.Account == account); if (user == null) { return(PoResult <ToDoList> .DbNotFound()); } var toDoList = this.Database.ToDoLists.Add(new ToDoList { Id = Guid.Create(), Name = listName, UserId = user.Id, Sort = user.ToDoLists.Count }); this.Database.SaveChanges(); return(PoResult <ToDoList> .PoSuccess(toDoList)); } catch (Exception e) { return(PoResult <ToDoList> .Exception(e)); } }
/// <summary> /// 使用帳號,取得使用者物件 /// </summary> /// <param name="account">帳號</param> /// <returns>使用者物件</returns> public PoResult <User> GetUser(string account) { try { var user = this.Database.Users.FirstOrDefault(o => o.Account == account); return(user == null ? PoResult <User> .DbNotFound() : PoResult <User> .PoSuccess(user)); } catch (Exception e) { return(PoResult <User> .Exception(e)); } }
/// <summary> /// 使用信箱,取得使用者 /// </summary> /// <param name="email">使用者的信箱</param> /// <returns>取得結果</returns> public PoResult <User> GetUserByEmail(string email) { try { var user = this.Database.Users.FirstOrDefault(o => o.Email == email); return(user == null ? PoResult <User> .DbNotFound() : PoResult <User> .PoSuccess(user)); } catch (Exception e) { return(PoResult <User> .Exception(e)); } }
/// <summary> /// 使用Id,取得使用者 /// </summary> /// <param name="userId">使用者Id</param> /// <returns>取得結果</returns> public PoResult <User> GetUser(Guid userId) { try { var user = this.Database.Users.Find(userId); return(user == null ? PoResult <User> .DbNotFound() : PoResult <User> .PoSuccess(user)); } catch (Exception e) { return(PoResult <User> .Exception(e)); } }
/// <summary> /// 使用待辦清單Id陣列、使用者帳號,更新待辦清單的排序 /// </summary> /// <param name="listIds">待辦清單Id陣列</param> /// <param name="userAccount">使用者帳號</param> /// <returns>更新結果</returns> public PoResult UpdateListSort(System.Guid[] listIds, string userAccount) { try { var index = 0; foreach (var listId in listIds) { var toDoList = this.Database.ToDoLists.FirstOrDefault(o => o.Id == listId && o.User.Account == userAccount); if (toDoList == null) { return(PoResult.DbNotFound()); } toDoList.Sort = index++; } this.Database.SaveChanges(); return(PoResult.PoSuccess()); } catch (Exception e) { return(PoResult.Exception(e)); } }