public static async Task AddDeviceAsync(int accountId, string username, string password) { using (var context = new Context()) { if (!Regex.IsMatch(username, "^[a-z0-9]+$", RegexOptions.IgnoreCase)) { throw new InvalidUsernameException(); } if (password.Length < 8) { throw new InvalidPasswordException(); } var account = (HumanAccount)await AccountsMgr.GetAccountAsync(accountId, context, onlyHuman : true); if (account.Devices.Any(d => d.Username == username)) { throw new UsernameTakenException(); } var device = new Device(); device.Account = account; device.Username = username; device.Password = password; device.Status = DeviceStatus.PENDING_CHANGES; context.Devices.Add(device); await context.SaveChangesAsync(); } }
public static async Task DeleteDeviceAsync(int accountId, int deviceId) { using (var context = new Context()) { var account = (HumanAccount)await AccountsMgr.GetAccountAsync(accountId, context, onlyHuman : true); var device = account.Devices.Where(d => d.Id == deviceId && d.Status != DeviceStatus.DELETED).FirstOrDefault(); if (device == null) { throw new InvalidDeviceException(); } device.Status = DeviceStatus.DELETED; await context.SaveChangesAsync(); } }