public async Task UpdateAsync(Contracts.Entities.Device entity)
 {
     using (var context = new ControlSystemContext.ControlSystemContext())
     {
         context.Update(entity);
         await context.SaveChangesAsync();
     }
 }
        public async Task <CreateDeviceResult> InsertAsync(Contracts.Entities.Device entity)
        {
            using (var context = new ControlSystemContext.ControlSystemContext())
            {
                var isUserExists = await _userRepository.IsUserExist(entity.UserId);

                if (!isUserExists)
                {
                    return new CreateDeviceResult()
                           {
                               Status = CreateDeviceStatus.UserNotExists
                           }
                }
                ;

                var isIndicatorsExists = await _indicatorRepository
                                         .IsIndicatorsExist(entity.DeviceIndicators.Select(ind => ind.IndicatorId));

                if (!isIndicatorsExists)
                {
                    return new CreateDeviceResult()
                           {
                               Status = CreateDeviceStatus.IndicatorNotExists
                           }
                }
                ;

                await context.AddAsync(entity);

                await context.SaveChangesAsync();

                return(new CreateDeviceResult()
                {
                    Status = CreateDeviceStatus.Success,
                    DeviceId = entity.Id
                });
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Create([FromBody] CreateDeviceRequest request)
        {
            if (request.UserId <= 0)
            {
                return(BadRequest("User id cann't be less or equal 0"));
            }

            if (!request.IndicatorIds.Any())
            {
                return(BadRequest("Device should have at list one minimum one indicator"));
            }

            var device = new Contracts.Entities.Device()
            {
                DeviceName       = request.DeviceName,
                UserId           = request.UserId,
                DeviceIndicators = request.IndicatorIds.Select(ind => new DeviceInicator()
                {
                    IndicatorId = ind
                }).ToList()
            };

            var result = await _deviceService
                         .CreateDevice(device);

            if (result.Status == CreateDeviceStatus.IndicatorNotExists)
            {
                return(NotFound($"Indicators not found"));
            }

            if (result.Status == CreateDeviceStatus.UserNotExists)
            {
                return(NotFound($"User with id: {request.UserId} not exist"));
            }

            return(Created("DevicesController", result));
        }
 public async Task UpdateDevice(Contracts.Entities.Device device)
 {
     await _deviceRepository.UpdateAsync(device);
 }
 public async Task <CreateDeviceResult> CreateDevice(Contracts.Entities.Device device)
 {
     return(await _deviceRepository.InsertAsync(device));
 }