Esempio n. 1
0
        public async Task<ApiMessage> Get()
        {
            DeviceOwner downer;
            Guid id = Guid.Empty;

            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);
                downer = new DeviceOwner() { device = new Device() { id = device.id } };
                authorized = await downer.FindByDeviceId();

                id = downer.user.id;
            }

            if (hasKey && authorized)
            {
                ShowAll show = new ShowAll();
                msg.data = await show.ListAll(id);
                msg.success = true;
            }
            else
            {
                msg.message = "Unauthorized";
            }
            return msg;
        }
Esempio n. 2
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;
        }
Esempio n. 3
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;
        }
Esempio n. 4
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;
        }
Esempio n. 5
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;
        }
Esempio n. 6
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;
        }
        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;
        }