Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        public async Task<ApiMessage> Post(GroupListCreateModel model)
        {
            ApiMessage msg = new ApiMessage() { success = false };
            GroupList group = new GroupList(model);
            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);
                group.device_id = device.id;
            }

            if (hasKey && authorized)
            {
                if (ModelState.IsValid)
                {
                    DeviceOwner downer = new DeviceOwner() { device = new Device() { id = group.device_id } };
                    bool founduser = await downer.FindByDeviceId();
                    bool success = await group.Create();
                    if (success)
                    {
                        Membership member = new Membership() { user_id = downer.user.id, group_id = group.id, status = MembershipStatus.Admin };
                        success = await member.Create();
                    }

                    if (success)
                    {
                        msg.message = "Group is created successfully";
                        msg.success = true;
                        msg.data = group.Return;
                    }
                    else
                    {
                        msg.message = "Failed to add group";
                    }
                }
                else
                {
                    msg.message = "Data is not completed";
                }
            }
            else
            {
                msg.message = "Unauthorized";
            }
            return msg;
        }
Ejemplo n.º 4
0
        public async Task<List<GroupListResponseModel>> ListByUserId(Guid user_id)
        {
            List<GroupListResponseModel> groups = new List<GroupListResponseModel>();
            Exception err = null;
            string sql = "SELECT grouplists.id, grouplists.name, grouplists.description, grouplists.status, grouplists.created_at FROM dbo.GroupLists WITH (NOLOCK), dbo.Memberships WITH (NOLOCK) WHERE grouplists.id = memberships.group_id AND memberships.user_id = @user_id AND grouplists.status = 1 AND memberships.status != 0";

            if (db.State != ConnectionState.Open)
                await db.OpenAsync();

            try
            {
                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand(sql, db);
                cmd.Parameters.AddWithValue("@user_id", user_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)
                    {
                        GroupList group = new GroupList();
                        group.id = (Guid)dr["id"];
                        group.name = dr["name"].ToString();
                        group.description = dr["description"].ToString();
                        group.status = (GroupStatus)dr["status"];
                        group.created_at = (DateTime)dr["created_at"];

                        Membership member = new Membership();
                        group.members = await member.ListGroupMember(group.id);

                        groups.Add(group.Return);
                    }
                }
            }
            catch (Exception e)
            {
                err = e;
            }
            finally
            {
                db.Close();
            }

            if (err != null)
            {
                throw err;
            }

            return groups;
        }
Ejemplo n.º 5
0
        public async Task<bool> FindById()
        {
            bool success = false;
            Exception err = null;
            string sql = "SELECT name, description, status, created_at FROM dbo.GroupLists WITH (NOLOCK) WHERE id LIKE @id";

            if (db.State != ConnectionState.Open)
                await db.OpenAsync();

            try
            {
                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand(sql, db);
                cmd.Parameters.AddWithValue("@id", id);
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(dt);

                if (dt != null && !dt.HasErrors && dt.Rows.Count > 0)
                {
                    this.name = dt.Rows[0]["name"].ToString();
                    this.description = dt.Rows[0]["description"].ToString();
                    this.status = (GroupStatus)dt.Rows[0]["status"];
                    this.created_at = (DateTime)dt.Rows[0]["created_at"];

                    Membership member = new Membership();
                    this.members = await member.ListGroupMember(this.id);

                    success = true;
                }
            }
            catch (Exception e)
            {
                err = e;
            }
            finally
            {
                db.Close();
            }

            if (err != null)
            {
                throw err;
            }

            return success;
        }
Ejemplo n.º 6
0
        public async Task<ApiMessage> Put(Guid id, GroupListCreateModel model)
        {
            ApiMessage msg = new ApiMessage() { success = false };
            GroupList group = new GroupList(model);
            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);
                group.device_id = device.id;

                DeviceOwner downer = new DeviceOwner() { device = new Device() { id = group.device_id } };
                await downer.FindByDeviceId();
                
                group.id = id;
                bool hasauthorized = await group.FindById();
                Membership member = new Membership() { user_id = downer.user.id, group_id = group.id };
                if (hasauthorized)
                { 
                    authorized = await member.FindByDeviceIdAndGroupId();
                    authorized = member.status == MembershipStatus.Admin ? true : false;
                }
            }

            if (hasKey && authorized)
            {
                if (ModelState.IsValid)
                {
                    group.name = model.name != null ? model.name : group.name;
                    group.description = model.description != null ? model.description : group.description;
                    group.status = model.status;
                    bool success = await group.Update();

                    if (success)
                    {
                        msg.message = "Group is updated successfully";
                        msg.success = true;
                        msg.data = group.Return;
                    }
                    else
                    {
                        msg.message = "Failed to update group";
                    }
                }
                else
                {
                    msg.message = "Data is not completed";
                }
            }
            else
            {
                msg.message = "Unauthorized";
            }
            return msg;
        }
Ejemplo n.º 7
0
        public async Task<ApiMessage> Get(Guid id)
        {
            ApiMessage msg = new ApiMessage() { success = false };
            IEnumerable<string> xAccessKey;
            bool hasKey = Request.Headers.TryGetValues("X-Access-Key", out xAccessKey);
            bool authorized = false;
            GroupList group = new GroupList() { id = id };

            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();
                
                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)
            {
                msg.data = group.Return;
                msg.success = true;
                msg.message = "Show group successfully";
            }
            else
            {
                msg.message = "Unauthorized";
            }

            return msg;
        }
Ejemplo n.º 8
0
        public async Task<ApiMessage> Get()
        {
            ApiMessage msg = new ApiMessage() { success = false };
            Membership member = new Membership();
            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();
                //Membership member = new Membership() { user_id = downer.user.id, group_id = id };
                member.user_id = downer.user.id;
            }

            if (hasKey && authorized)
            {
                GroupList group = new GroupList();
                msg.data = await group.ListByUserId(member.user_id);
                msg.success = true;
                msg.message = "List group successfully";
            }
            else
            {
                msg.message = "Unauthorized";
            }

            return msg;
        }
Ejemplo n.º 9
0
        public async Task<ApiMessage> Put(Guid id, MembershipCreateModel model)
        {
            ApiMessage msg = new ApiMessage() { success = false };

            GroupList group = new GroupList();
            IEnumerable<string> xAccessKey;
            bool hasKey = Request.Headers.TryGetValues("X-Access-Key", out xAccessKey);
            bool authorized = false;
            Device device = new Device();

            if (hasKey)
            {
                device = new Device() { access_key = xAccessKey.First() };
                authorized = await device.FindByAccessKey(device.access_key, true);
                group.id = id;
                bool hasauthorized = await group.FindById();

                DeviceOwner downer = new DeviceOwner() { device = new Device() { id = device.id } };
                bool founduser = await downer.FindByDeviceId();
                Membership member = new Membership() { user_id = downer.user.id, group_id = group.id };
                if (hasauthorized)
                {
                    authorized = await member.FindByDeviceIdAndGroupId();
                    authorized = member.status == MembershipStatus.Admin ? true : false;
                }
            }

            if (hasKey && authorized)
            {
                if (ModelState.IsValid)
                {
                    bool success = await group.FindById();
                    if (success)
                    {
                        //Find user id
                        User user = new User() { facebook_id = model.facebook_id };
                        bool foundUser = await user.FindByFacebookID();

                        //if not found then create a temporary
                        if (!foundUser)
                        {
                            msg.message = "User is not exists";
                        }
                        else
                        {
                            Membership member = new Membership() { user_id = user.id, group_id = group.id };
                            bool foundMember = await member.FindByDeviceIdAndGroupId();
                            if (foundMember && member.status != model.status)
                            {
                                member.status = model.status;
                                success = await member.Update();
                                if (success)
                                {
                                    msg.message = "Member is updated successfully";
                                    msg.success = true;
                                    msg.data = user.Return;
                                }
                                else
                                {
                                    msg.message = "Failed to update a member";
                                }
                            }
                            else
                            {
                                msg.message = "User is not in the group.";
                                msg.data = user.Return;
                            }
                        }
                    }
                }
                else
                {
                    msg.message = "Data is not completed";
                }
            }
            else
            {
                msg.message = "Unauthorized";
            }
            return msg;
        }