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; }
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; }
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<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; }
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; }
public async Task<ApiMessage> Post(UserRegisterModel model) { ApiMessage msg = new ApiMessage() { success = false }; try { if (ModelState.IsValid) { UserResponseModel response; if (!UniTool.VerifyFacebook(model.facebook_id, model.access_token, out response)) { msg.message = "Sorry, Facebook access token is invalid"; return msg; } User user = new User(response); bool success = await user.FindByFacebookID(); if (success) { await user.FindByID(); msg.message = "This user had been registered before"; msg.success = true; msg.data = user.Return; } else { user.isAnonymous = false; if (await user.Register()) { msg.message = "User has been registered successfully"; msg.success = true; msg.data = user.Return; } } } else { msg.message = "data is not completed"; } } catch (Exception e) { msg.message = e.Message; } return msg; }
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; }
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> 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; }
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; }