public async Task <ActionResult <ProgramResponse> > AddProgram(Guid deviceId, [FromBody] ProgramRequest req) { var user = HttpContext.User.Identity?.Name; if (user == null) { return(NotFound("Cannot find user")); } var ud = await Db.Users.Where(x => x.Aud == user) .Include(x => x.Device) .FirstOrDefaultAsync(x => x.Device.DeviceId == deviceId); if (ud == null) { return(NotFound("Device Not Found")); } var pd = new ProgramData { Name = req.Name, Code = req.Code, DeviceId = ud.DeviceId, }; var saved = await Db.Programs .AddAsync(pd); await Db.SaveChangesAsync(); var ok = saved.Entity !; return(Ok(new ProgramResponse(ok.Id, ok.Name, ok.Code))); }
public async Task <ActionResult <TriggerResponse> > UpdateProgram(Guid deviceId, Guid programId, [FromBody] ProgramRequest req) { var user = HttpContext.User.Identity?.Name; if (user == null) { return(NotFound("Cannot find user")); } var ud = await Db.Users.Where(x => x.Aud == user) .Include(x => x.Device) .FirstOrDefaultAsync(x => x.Device.DeviceId == deviceId); if (ud == null) { return(NotFound("Device Not Found")); } var td = await Db.Programs.Include(x => x.Device) .FirstOrDefaultAsync(x => x.Device.DeviceId == deviceId && x.Id == programId); if (td == null) { return(NotFound("Program Not Found")); } td.Code = req.Code; td.Name = req.Name; await Db.SaveChangesAsync(); return(Ok(new TriggerResponse(td.Id, td.Name))); }