public async Task <IActionResult> Create(CreateViewModel model) { // TODO: Must check CompanyKey to ensure it was our module that sent the information // if (CompanyKey != OurKey) { return BadRequest() unauthorized? } if (ModelState.IsValid) { var storedDLM = await _repository.GetDLMByGuid(model.Id); if (storedDLM != null) { return(BadRequest($"Module with id: {model.Id} already created")); } var idm = new IDM { Id = model.Id, IsClosed = model.IsClosed }; _repository.AddIDM(idm); return(Ok()); } return(BadRequest(ModelState)); }
public async void RegisterIDM(Guid moduleId, string homeId, string homePassword, string nickname) { var home = _repository.GetHomeByNameAndPassword(homeId, homePassword); if (home == null) { return; } var idm = await _repository.GetIDMByGuid(moduleId); if (idm != null) { return; } var newIdm = new IDM { Id = moduleId, IsClosed = false, AssociatedHomeId = home.Id, Nickname = nickname }; _repository.AddIDM(newIdm); home.IDMs.Add(newIdm); await _repository.Commit(); }
public async Task <IActionResult> SeedDatabase() { if (!_context.Users.IsNullOrEmpty() || !_context.Homes.IsNullOrEmpty() || !_context.DLMs.IsNullOrEmpty() || !_context.IDMs.IsNullOrEmpty()) { return(BadRequest("Database must be empty.")); } var user1 = new ApplicationUser { UserName = "******", Email = "*****@*****.**", FirstName = "Robert", LastName = "Taylor", IsHome = true }; var user2 = new ApplicationUser { UserName = "******", Email = "*****@*****.**", FirstName = "Ryan", LastName = "Becker", IsHome = false }; await _userManager.CreateAsync(user1, "Testing123!"); await _userManager.CreateAsync(user2, "Testing123!"); var dlm1 = new DLM { Id = new Guid("66155e63-3b3d-41fc-a3ff-34cd7b521a59"), Nickname = "Front Door", IsLocked = true, AssociatedHomeId = 1 }; var dlm2 = new DLM { Id = new Guid("229371a2-0af0-4514-ade7-ad339f94ced4"), Nickname = "Back Door", IsLocked = false, AssociatedHomeId = 1 }; await _context.DLMs.AddAsync(dlm1); await _context.DLMs.AddAsync(dlm2); var idm1 = new IDM { Id = new Guid("e9c7aa44-0653-4f5e-b5b7-0aa7c2fdeea6"), Nickname = "Bedroom Window", IsClosed = true, AssociatedHomeId = 1 }; var idm2 = new IDM { Id = new Guid("cc46356a-2ea8-4fd9-b4d2-1305c974c788"), Nickname = "Kitchen Window", IsClosed = false, AssociatedHomeId = 1 }; await _context.IDMs.AddAsync(idm1); await _context.IDMs.AddAsync(idm2); var passwordHasher = new Rfc2898DeriveBytes("Testing123!", 256); var home = new Home { Id = 1, Name = "TestHome", Nickname = "Test Home", PasswordHash = passwordHasher.GetBytes(256), Salt = passwordHasher.Salt, Latitude = 40.759211, Longitude = -73.984638, Users = new List <ApplicationUser> { user1, user2 }, DLMs = new List <DLM> { dlm1, dlm2 }, IDMs = new List <IDM> { idm1, idm2 } }; await _context.Homes.AddAsync(home); user1.HomeId = 1; user2.HomeId = 1; await _context.SaveChangesAsync(); var storedHome = await _context.Homes.FindAsync(1); var mappedHome = _mapper.Map <Home, HomeStatusViewModel>(storedHome); return(Ok(mappedHome)); }
public async void AddIDM(IDM idm) { await _context.IDMs.AddAsync(idm); await Commit(); }
public void RemoveIdm(IDM idm) { _context.IDMs.Remove(idm); }