public async Task TrashInspectionModel_UpdateWFalse_DoesUpdate() { // Arrange Random rnd = new Random(); TrashInspection trashInspection = new TrashInspection { CreatedAt = DateTime.Now, Date = DateTime.Now, EakCode = rnd.Next(1, 255).ToString(), MustBeInspected = false, Producer = Guid.NewGuid().ToString(), RegistrationNumber = Guid.NewGuid().ToString(), Time = DateTime.Now, Transporter = Guid.NewGuid().ToString(), TrashFraction = rnd.Next(1, 255).ToString(), WeighingNumber = rnd.Next(1, 255).ToString() }; await trashInspection.Create(DbContext); // Act trashInspection.CreatedAt = trashInspection.CreatedAt; trashInspection.Date = trashInspection.Date; trashInspection.EakCode = trashInspection.EakCode; trashInspection.Id = trashInspection.Id; trashInspection.InstallationId = trashInspection.InstallationId; trashInspection.MustBeInspected = trashInspection.MustBeInspected; trashInspection.Producer = trashInspection.Producer; trashInspection.RegistrationNumber = trashInspection.RegistrationNumber; trashInspection.Time = trashInspection.Time; trashInspection.Transporter = trashInspection.Transporter; trashInspection.TrashFraction = trashInspection.TrashFraction; trashInspection.WeighingNumber = trashInspection.WeighingNumber; await trashInspection.Update(DbContext); TrashInspection dbTrashInspection = DbContext.TrashInspections.AsNoTracking().First(); List <TrashInspection> trashInspectionList = DbContext.TrashInspections.AsNoTracking().ToList(); // Assert Assert.NotNull(dbTrashInspection); Assert.AreEqual(1, trashInspectionList.Count()); Assert.AreEqual(trashInspection.CreatedAt.ToString(), dbTrashInspection.CreatedAt.ToString()); Assert.AreEqual(trashInspection.Date.ToString(), dbTrashInspection.Date.ToString()); Assert.AreEqual(trashInspection.EakCode, dbTrashInspection.EakCode); Assert.AreEqual(trashInspection.InstallationId, dbTrashInspection.InstallationId); Assert.AreEqual(trashInspection.MustBeInspected, dbTrashInspection.MustBeInspected); Assert.AreEqual(trashInspection.Producer, dbTrashInspection.Producer); Assert.AreEqual(trashInspection.RegistrationNumber, dbTrashInspection.RegistrationNumber); Assert.AreEqual(trashInspection.Time.ToString(), dbTrashInspection.Time.ToString()); Assert.AreEqual(trashInspection.Transporter, dbTrashInspection.Transporter); Assert.AreEqual(trashInspection.TrashFraction, dbTrashInspection.TrashFraction); Assert.AreEqual(trashInspection.WeighingNumber, dbTrashInspection.WeighingNumber); }
public async Task TrashInspectionModel_SaveWTrue_DoesSave() { // Arrange Random rnd = new Random(); TrashInspection trashInspectionModel = new TrashInspection { CreatedAt = DateTime.Now, Date = DateTime.Now, EakCode = rnd.Next(1, 255).ToString(), MustBeInspected = true, Producer = Guid.NewGuid().ToString(), RegistrationNumber = Guid.NewGuid().ToString(), Time = DateTime.Now, Transporter = Guid.NewGuid().ToString(), TrashFraction = rnd.Next(1, 255).ToString(), WeighingNumber = rnd.Next(1, 255).ToString() }; // Act await trashInspectionModel.Create(DbContext); TrashInspection trashInspection = DbContext.TrashInspections.AsNoTracking().First(); List <TrashInspection> trashInspectionList = DbContext.TrashInspections.AsNoTracking().ToList(); // Assert Assert.NotNull(trashInspection); Assert.AreEqual(1, trashInspectionList.Count()); Assert.AreEqual(trashInspectionModel.CreatedAt.ToString(), trashInspection.CreatedAt.ToString()); Assert.AreEqual(trashInspectionModel.CreatedByUserId, trashInspection.CreatedByUserId); Assert.AreEqual(trashInspectionModel.Date.ToString(), trashInspection.Date.ToString()); Assert.AreEqual(trashInspectionModel.EakCode, trashInspection.EakCode); Assert.AreEqual(trashInspectionModel.InstallationId, trashInspection.InstallationId); Assert.AreEqual(trashInspectionModel.MustBeInspected, trashInspection.MustBeInspected); Assert.AreEqual(trashInspectionModel.Producer, trashInspection.Producer); Assert.AreEqual(trashInspectionModel.RegistrationNumber, trashInspection.RegistrationNumber); Assert.AreEqual(trashInspectionModel.Time.ToString(), trashInspection.Time.ToString()); Assert.AreEqual(trashInspectionModel.Transporter, trashInspection.Transporter); Assert.AreEqual(trashInspectionModel.TrashFraction, trashInspection.TrashFraction); Assert.AreEqual(trashInspectionModel.WeighingNumber, trashInspection.WeighingNumber); }
public async Task <OperationResult> Create(TrashInspectionModel createModel) { var dateTime = DateTime.UtcNow; var pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "TrashInspectionBaseSettings:Token"); if (pluginConfiguration == null) { return(new OperationResult(false)); } if (createModel.Token == pluginConfiguration.Value && createModel.WeighingNumber != null) { try { // Handling the situation, where incoming timestamp is not in UTC. var utcAdjustment = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "TrashInspectionBaseSettings:UtcAdjustment"); if (utcAdjustment.Value == "true") { if (createModel.Time.Hour > dateTime.Hour) { var timeSpan = createModel.Time.Subtract(dateTime); var minutes = timeSpan.Hours * 60.0 + timeSpan.Minutes; var hours = minutes / 60.0; var fullHours = Math.Round(hours); createModel.Time = createModel.Time.AddHours(-fullHours); } } TrashInspection trashInspection = await _dbContext.TrashInspections.FirstOrDefaultAsync(x => x.WeighingNumber == createModel.WeighingNumber); if (trashInspection != null) { return(new OperationResult(true, trashInspection.Id.ToString())); } trashInspection = new TrashInspection { WeighingNumber = createModel.WeighingNumber, Date = createModel.Date, Time = createModel.Time, EakCode = createModel.EakCode, ExtendedInspection = createModel.ExtendedInspection, RegistrationNumber = createModel.RegistrationNumber, TrashFraction = createModel.TrashFraction, Producer = createModel.Producer, Transporter = createModel.Transporter, MustBeInspected = createModel.MustBeInspected, InspectionDone = false, Status = 0, UpdatedByUserId = _userService.UserId, CreatedByUserId = _userService.UserId, }; await trashInspection.Create(_dbContext); var segment = _dbContext.Segments.FirstOrDefault(x => x.Name == createModel.Segment); var installation = _dbContext.Installations.FirstOrDefault(x => x.Name == createModel.InstallationName); var fraction = _dbContext.Fractions.FirstOrDefault(x => x.ItemNumber == createModel.TrashFraction); _coreHelper.LogEvent($"CreateTrashInspection: Segment: {createModel.Segment}, InstallationName: {createModel.InstallationName}, TrashFraction: {createModel.TrashFraction} "); if (segment != null && installation != null && fraction != null) { trashInspection.SegmentId = segment.Id; trashInspection.FractionId = fraction.Id; trashInspection.InstallationId = installation.Id; await trashInspection.Update(_dbContext); createModel.SegmentId = segment.Id; createModel.FractionId = fraction.Id; createModel.InstallationId = installation.Id; createModel.Id = trashInspection.Id; await UpdateProducerAndTransporter(trashInspection, createModel); _coreHelper.LogEvent($"CreateTrashInspection: Segment: {segment.Name}, InstallationName: {installation.Name}, TrashFraction: {fraction.Name} "); await _bus.SendLocal(new TrashInspectionReceived(createModel, fraction.Id, segment.Id, installation.Id)); } return(new OperationResult(true, createModel.Id.ToString())); } catch (Exception ex) { Console.WriteLine(ex.Message); return(new OperationResult(false)); } } return(new OperationResult(false)); }