示例#1
0
        public async Task <int> Update(MotorUpdateDto dto, int id, string userId)
        {
            var oldMotor = await _dbContext.Motors
                           .Include(x => x.Client)
                           .Include(x => x.MaintenanceCycles)
                           .Include(x => x.MaintenanceContracts)
                           .Include(x => x.MaintenanceServices)
                           .Include(x => x.SaleContracts)
                           .Include(x => x.SparePartsSold)
                           .SingleOrDefaultAsync(x => x.Id == id);

            var updatedMotor = _mapper.Map(dto, oldMotor);

            updatedMotor.UpdateAt  = DateTime.Now;
            updatedMotor.UpdatedBy = userId;

            _dbContext.Motors.Update(updatedMotor);

            await _dbContext.SaveChangesAsync();

            // Check Motor if Need to Change Oli

            var hoursDifference = updatedMotor.CurrentCounterReading - updatedMotor.PreviousCounterReading;

            if (hoursDifference >= updatedMotor.OliCounter)
            {
                // Motor Need to Change Oli , Then Push Notification To All Users

                var usersFcmToken = await _dbContext.Users
                                    .Where(x => x.FcmToken != null)
                                    .Select(x => x.FcmToken).ToListAsync();

                var message = new MessageCreateDto()
                {
                    Title    = "اشعار صيانة",
                    Body     = "هناك مولد يحتاج الى تغيير زيت للمحرك",
                    Action   = "Motor",
                    ActionId = updatedMotor.Id
                };

                var notifications = _service.CreateNotifications(message, usersFcmToken);

                await _service.PushNotifications(notifications);
            }


            return(updatedMotor.Id);
        }
示例#2
0
 public async Task <IActionResult> Update([FromBody] MotorUpdateDto dto, int id)
 => await GetResponse(async (userId) =>
                      new ApiResponseViewModel(true, "Motor Updated Successfully", await _service.Update(dto, id, userId)));