Ejemplo 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;
        }
Ejemplo n.º 2
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.º 3
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.º 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;
        }
Ejemplo n.º 5
0
        public async Task<ApiMessage> Post(DeviceRegisterModel model)
        {
            ApiMessage msg = new ApiMessage() { success = false, data = model };
            try
            {
                if (ModelState.IsValid)
                {
                    Device device = new Device(model);

                    bool success = await device.FindByUUID();
                    if (success)
                    {
                        await device.FindByID();

                        if (model.app_token != null)
                        {
                            device.app_token = model.app_token;
                        }
                        if (model.user_id != null && model.user_id != Guid.Empty)
                        {
                            device.user_id = model.user_id;
                        }

                        await device.UpdateInstall();
                        msg.message = "This device had been registered before";
                        msg.success = true;
                        msg.data = device.Return;
                    }
                    else
                    {
                        if (await device.Install())
                        {
                            msg.message = "This device has been registered successfully";
                            msg.success = true;
                            msg.data = device.Return;
                        }
                    }
                }
                else
                {
                    msg.message = "data is not completed";
                }
            }
            catch (Exception e)
            {
                msg.message = e.Message;
            }
            return msg;
        }
Ejemplo n.º 6
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.º 7
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.º 8
0
        public async Task<bool> FindByDeviceId()
        {
            bool success = false;

            Exception err = null;
            string sql = "SELECT devices.id as device_id, devices.uuid, devices.os, devices.model, devices.installed_at, users.id as user_id, users.facebook_id, users.gender, users.email, users.name, users.first_name, users.last_name FROM dbo.DeviceOwners WITH (NOLOCK), dbo.Devices WITH (NOLOCK), dbo.Users WITH (NOLOCK) WHERE deviceowners.device_id = devices.id AND deviceowners.user_id = users.id AND deviceowners.device_id = @device_id ORDER BY linked_at DESC";

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

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

                if (dt != null && !dt.HasErrors && dt.Rows.Count > 0)
                {
                    User newUser = new User();
                    newUser.id = (Guid)dt.Rows[0]["user_id"];
                    newUser.facebook_id = dt.Rows[0]["facebook_id"].ToString();
                    newUser.gender = dt.Rows[0]["gender"].ToString();
                    newUser.email = dt.Rows[0]["email"].ToString();
                    newUser.name = dt.Rows[0]["name"].ToString();
                    newUser.first_name = dt.Rows[0]["first_name"].ToString();
                    newUser.last_name = dt.Rows[0]["last_name"].ToString();
                    
                    Device newDevice = new Device();
                    newDevice.id = (Guid)dt.Rows[0]["device_id"];
                    newDevice.uuid = dt.Rows[0]["uuid"].ToString();
                    newDevice.os = dt.Rows[0]["os"].ToString();
                    newDevice.model = dt.Rows[0]["model"].ToString();
                    newDevice.installed_at = (DateTime)dt.Rows[0]["installed_at"];

                    this.device = newDevice;
                    this.user = newUser;

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

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

            return success;
        }
Ejemplo n.º 9
0
        public async Task<ApiMessage> Post(LinkDeviceRegisterModel model)
        {
            ApiMessage msg = new ApiMessage() { success = false, data = model };
            try
            {
                //scenario 1: provided with Device id and User id
                if (model.user.id != null && model.device.id != null && model.user.id != Guid.Empty && model.device.id != Guid.Empty)
                {
                    Device device = new Device() { id = model.device.id };
                    User user = new User() { id = model.user.id };
                    if (await user.FindByID() && await device.FindByID())
                    {
                        DeviceOwner downer = new DeviceOwner() { user = user, device = device };

                        if (await downer.FindByDeviceAndUserId())
                        {
                            msg.success = false;
                            msg.message = "Device and User had been linked before";
                            msg.data = downer.Return;
                        }
                        else if (await downer.LinkDevice())
                        {
                            msg.success = true;
                            msg.message = "Device and User is linked successfully";
                            msg.data = downer.Return;
                        }
                        else
                        {
                            msg.message = "Device and User is failed to link";
                        }
                    }
                    else
                    {
                        msg.message = "Device or User is not registered yet.";
                    }
                }
                else //scenario 2: register user and device
                {
                    Device device = new Device() { uuid = model.device.uuid, os = model.device.os, model = model.device.model, app_token = model.device.app_token, user_id = model.device.user_id };
                    User user = new User() { facebook_id = model.user.facebook_id, access_token = model.user.access_token };

                    DeviceOwner downer = new DeviceOwner() { user = user, device = device };

                    UserResponseModel response;
                    if (!UniTool.VerifyFacebook(user.facebook_id, user.access_token, out response))
                    {
                        msg.message = "Sorry, Facebook access token is invalid";
                        return msg;
                    }
                    user = new User(response);

                    //try find user and device first
                    bool installed = true;
                    if (!await device.FindByUUID())
                    {
                        installed = await device.Install();
                    }
                    else
                    {
                        await device.FindByID();
                    }
                    if (installed && !await user.FindByFacebookID())
                    {
                        installed = await user.Register();
                    }

                    downer.device = device;
                    downer.user = user;

                    //try register user and device first
                    if (installed)
                    {
                        if (await downer.FindByDeviceAndUserId())
                        {
                            msg.success = true;
                            msg.message = "Device and User had been linked before";
                            downer.user = user;
                            downer.device = device;
                            msg.data = downer.Return;
                        }
                        else if (await downer.LinkDevice())
                        {
                            msg.success = true;
                            msg.message = "Device and User is linked successfully";
                            downer.user = user;
                            downer.device = device;
                            msg.data = downer.Return;
                        }
                        else
                        {
                            msg.message = "Device and User is failed to link";
                        }
                    }
                    else
                    {
                        msg.message = "Device and User is failed to be created";
                    }
                }
            }
            catch (Exception e)
            {
                msg.message = e.Message;
            }
            return msg;
        }
Ejemplo n.º 10
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;
        }