public IHttpActionResult AssigningDevices(DeviceandUsersModel model) { try { if (ModelState.IsValid) { var validdevice = db.DevicePasswords.Where(s => s.DeviceId == model.DeviceId).ToList(); var validuser = db.Users.Where(s => s.UserId == model.UserId).ToList(); //var chkexists = db.DeviceforUsers.Where(s => s.DeviceId==model.DeviceId || s.UserId==model.UserId).ToList(); if ((validdevice.Count != 0) && (validuser.Count != 0)) { var adddeviceusers = new DeviceforUser(); adddeviceusers.DeviceId = model.DeviceId; adddeviceusers.UserId = model.UserId; adddeviceusers.FirstName = model.FirstName; db.DeviceforUsers.Add(adddeviceusers); db.SaveChanges(); return(Ok(adddeviceusers)); } else if (validdevice.Count == 0) { return(Ok("Device Not Exists")); } else if (validuser.Count == 0) { return(Ok("User Not Exists")); } else { return(NotFound()); } } else { return(Ok("Please Enter All the mandatory Fields")); } } catch (Exception ex) { return(NotFound()); } }
public IHttpActionResult RevokeAssigningDevices(DeviceandUsersModel model) { try { var validdevice = db.DevicePasswords.Where(s => s.DeviceId == model.DeviceId).ToList(); var validuser = db.Users.Where(s => s.UserId == model.UserId).ToList(); var device = db.DeviceforUsers.Where(s => s.DeviceId == model.DeviceId).FirstOrDefault(); if ((validdevice.Count != 0) && (validuser.Count != 0) && (device != null)) { device.UserId = model.UserId; device.FirstName = model.FirstName; db.SaveChanges(); return(Ok(device)); } else if (validdevice.Count == 0) { return(Ok("Device Not Exists")); } else if (validuser.Count == 0) { return(Ok("User Not Exists")); } else if (device == null) { return(Ok("The device till not assigned. Need to assign first.")); } else { return(NotFound()); } } catch (Exception ex) { return(NotFound()); } }