public async Task<All_UserModel> ListAll(Guid userid) { All_UserModel userModel = new All_UserModel(); //get user User user = new User() { id = userid }; if (await user.FindByID()) { userModel.id = user.id; userModel.facebook_id = user.facebook_id; userModel.gender = user.gender; userModel.email = user.email; userModel.name = user.name; userModel.first_name = user.first_name; userModel.last_name = user.last_name; } //get group GroupList group = new GroupList(); List<GroupListResponseModel> groups = await group.ListByUserId(user.id); userModel.groups = new List<All_GroupModel>(); foreach (GroupListResponseModel g in groups) { All_GroupModel groupModel = new All_GroupModel(); groupModel.id = g.id; groupModel.name = g.name; groupModel.description = g.description; groupModel.status = g.status; groupModel.created_at = g.created_at; //get group members Membership member = new Membership(); groupModel.members = await member.ListGroupMember(g.id); //get items in each group ItemList item = new ItemList(); List<ItemListResponseModel> items = await item.ListByGroupId(g.id); groupModel.items = new List<All_ItemModel>(); foreach (ItemListResponseModel i in items) { All_ItemModel itemModel = new All_ItemModel(); itemModel.id = i.id; itemModel.name = i.name; itemModel.description = i.description; itemModel.status = i.status; itemModel.created_at = i.created_at; itemModel.category = i.category; itemModel.category_id = i.category_id; itemModel.created_by = i.created_by; groupModel.items.Add(itemModel); } userModel.groups.Add(groupModel); } return userModel; }
public async Task<ApiMessage> Post(Guid id, ItemListCreateModel model) { ItemList item = new ItemList(model); ApiMessage msg = new ApiMessage() { success = false }; IEnumerable<string> xAccessKey; bool hasKey = Request.Headers.TryGetValues("X-Access-Key", out xAccessKey); bool authorized = false; if (hasKey) { Device device = new Device() { access_key = xAccessKey.First() }; authorized = await device.FindByAccessKey(device.access_key, true); DeviceOwner downer = new DeviceOwner() { device = new Device() { id = device.id } }; authorized = await downer.FindByDeviceId(); GroupList group = new GroupList() { id = id }; bool hasauthorized = await group.FindById(); Membership member = new Membership() { user_id = downer.user.id, group_id = id }; if (hasauthorized) { authorized = await member.FindByDeviceIdAndGroupId(); authorized = member.status == MembershipStatus.Kicked ? false : true; item.group_id = group.id; item.created_by = downer.user.id; item.creator = downer.user; } } if (hasKey && authorized) { if (ModelState.IsValid) { bool success = await item.Create(); if (success) { msg.message = "Item is created successfully"; msg.success = true; msg.data = item.Return; } else { msg.message = "Failed to add item"; } } else { msg.message = "Data is not completed"; } } else { msg.message = "Unauthorized"; } return msg; }
public async Task<List<ItemListResponseModel>> ListByGroupId(Guid group_id) { List<ItemListResponseModel> items = new List<ItemListResponseModel>(); Exception err = null; string sql = "SELECT id, name, description, status, created_at, created_by, category, category_id FROM dbo.ItemLists WITH (NOLOCK) WHERE group_id = @group_id"; if (db.State != ConnectionState.Open) await db.OpenAsync(); try { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(sql, db); cmd.Parameters.AddWithValue("@group_id", group_id); SqlDataAdapter adp = new SqlDataAdapter(); adp.SelectCommand = cmd; adp.Fill(dt); if (dt != null && !dt.HasErrors && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { ItemList item = new ItemList(); item.id = (Guid)dr["id"]; item.name = dr["name"].ToString(); item.description = dr["description"].ToString(); item.status = (ItemStatus)dr["status"]; item.created_at = (DateTime)dr["created_at"]; item.created_by = (Guid)dr["created_by"]; item.category = dr["category"].ToString(); item.category_id = (Guid)dr["category_id"]; User aUser = new User() { id = item.created_by }; await aUser.FindByID(); item.creator = aUser; items.Add(item.Return); } } } catch (Exception e) { err = e; } finally { db.Close(); } if (err != null) { throw err; } return items; }
public async Task<ApiMessage> Get(Guid id, Guid iid) { ApiMessage msg = new ApiMessage() { success = false }; IEnumerable<string> xAccessKey; bool hasKey = Request.Headers.TryGetValues("X-Access-Key", out xAccessKey); bool authorized = false; if (hasKey) { Device device = new Device() { access_key = xAccessKey.First() }; authorized = await device.FindByAccessKey(device.access_key, true); DeviceOwner downer = new DeviceOwner() { device = new Device() { id = device.id } }; authorized = await downer.FindByDeviceId(); GroupList group = new GroupList() { id = id }; bool hasauthorized = await group.FindById(); Membership member = new Membership() { user_id = downer.user.id, group_id = id }; if (hasauthorized) { authorized = await member.FindByDeviceIdAndGroupId(); authorized = member.status == MembershipStatus.Kicked ? false : true; } } if (hasKey && authorized) { ItemList item = new ItemList() { id = iid }; bool success = await item.FindById(); if (success) { msg.data = item.Return; msg.success = true; msg.message = "Get item successfully"; } else { msg.message = "Failed to get item"; } } else { msg.message = "Unauthorized"; } return msg; }