public async Task <ServiceResult> AddSecurityDevice(long groupId, GroupSecurityDeviceCreateDto dto) { var user = await _userService.GetUser(); if (user == null) { return(ServiceResult.Error(ErrorsEnum.UserB2CMissing)); } var group = await _db.Groups .Include(g => g.GroupSecurityDevices) .Where(g => g.Id == groupId && g.OwnerId == user.Id) .FirstOrDefaultAsync(); if (group == null) { return(ServiceResult.Error(ErrorsEnum.GroupNotFound)); } var newDevice = await _ioTService.RegisterNewDevice(); group.GroupSecurityDevices.Add(new GroupSecurityDevice { DeviceId = newDevice.DeviceId, DeviceKey = newDevice.DeviceKey, GroupId = groupId, Name = dto.Name }); await _db.SaveChangesAsync(); return(ServiceResult.Ok(_mapper.Map <GroupSecurityDeviceDto>(group.GroupSecurityDevices.Last()))); }
public async Task <IActionResult> AddGroupDevice([FromRoute] long groupId, [FromBody] GroupSecurityDeviceCreateDto device) { var result = await _groupService.AddSecurityDevice(groupId, device); if (!result.Succeeded) { return(BadRequest(result.ErrorResult)); } return(Ok(result.Content as GroupSecurityDeviceDto)); }