public Domain.Observation Add( IValidationDictionary validationDictionary, Domain.Observation observation, HttpPostedFileBase postedFile) { if (!Validate(validationDictionary, observation)) { return(null); } SetObservationValues(observation); var data = _observationRepository.Create(); _observationRepository.Add(data); Mapper.Map(observation, data); //Reset the observation ID so that consumers cannot set this. data.ObservationId = 0; SaveFile(data, postedFile); _context.SaveChanges(); //Map back to the domain so we can use the full //result in the web site for sending email, etc. return(Mapper.Map <Domain.Observation>(data)); }
private static void SetObservationValues(Domain.Observation observation) { //Currently just map the time from the observation onto to the single //date property within the data layer. observation.ObservationDate = observation.ObservationDate .AddHours(observation.ObservationTime.Hour) .AddMinutes(observation.ObservationTime.Minute); }
public async Task <Result <Guid> > Handle(CreateObservationCommand request, CancellationToken token) { if ((request.BatchIds == null || !request.BatchIds.Any()) && (request.ProductIds == null || !request.ProductIds.Any())) { return(Failure <Guid>("Vous devez préciser au moins un produit ou un lot pour faire une observation.")); } var batches = request.BatchIds != null ? await _context.Batches .Where(b => request.BatchIds.Contains(b.Id)) .ToListAsync(token) : new List <Domain.Batch>(); var products = request.ProductIds != null ? await _context.Products .Where(b => request.ProductIds.Contains(b.Id)) .ToListAsync(token) : new List <Domain.Product>(); var batchProducerIds = batches.Select(b => b.ProducerId).Distinct().ToList(); if (batchProducerIds.Count > 1) { return(Failure <Guid>("Une observation est liée à un producteur, les lots doivent donc appartenir au même producteur.")); } var productProducerIds = products.Select(b => b.ProducerId).Distinct().ToList(); if (productProducerIds.Count > 1) { return(Failure <Guid>("Une observation est liée à un producteur, les produits doivent donc appartenir au même producteur.")); } var producerId = productProducerIds.Any() ? productProducerIds.First() : batchProducerIds.First(); var producer = await _context.Producers.SingleOrDefaultAsync(p => p.Id == producerId, token); var user = await _context.Users.SingleOrDefaultAsync(u => u.Id == request.RequestUser.Id, token); var entity = new Domain.Observation(Guid.NewGuid(), request.Comment, user, producer); entity.SetBatches(batches); entity.SetProducts(products); entity.SetVisibility(request.VisibleToAll); await _context.AddAsync(entity, token); await _context.SaveChangesAsync(token); return(Success(entity.Id)); }
public Domain.Observation Edit( IValidationDictionary validationDictionary, Domain.Observation observation, HttpPostedFileBase postedFile) { if (!Validate(validationDictionary, observation)) { return(null); } SetObservationValues(observation); var data = _observationRepository.GetById(observation.ObservationId); data = Mapper.Map(observation, data); SaveFile(data, postedFile); _context.SaveChanges(); //Map back to the domain so we can use the full //result in the web site for sending email, etc. return(Mapper.Map <Domain.Observation>(data)); }
public static ObservationMailerModel GetNotificationData(this Domain.Observation observation, string observationId, string url, string comment, string producerId) { return(new ObservationMailerModel { ProducerId = producerId, Producer = observation.Producer.Name, User = observation.User.Name, Comment = comment, Batches = observation.Batches?.Select(b => new BatchMailerModel { Number = b.Batch.Number, DDM = b.Batch.DDM, DLC = b.Batch.DLC, }).ToList() ?? new List <BatchMailerModel>(), Products = observation.Products?.Select(b => new ProductMailerModel { Name = b.Name, Reference = b.Reference }).ToList() ?? new List <ProductMailerModel>(), CreatedOn = observation.CreatedOn, ObservationId = observationId, PortalUrl = url, }); }
/// <summary> /// Performs field logic that cannot be represented with attributes for an observation. /// </summary> /// <param name="validationDictionary">A dictionary that will contain the resulting errors</param> /// <param name="observation">The observation to validate</param> /// <returns><c>true</c> for success, <c>false</c> for failure</returns> /// <remarks> /// This function should be used in conjunction with the built-in data annotations to validate /// simple conditions such as required. /// </remarks> public bool Validate(IValidationDictionary validationDictionary, Domain.Observation observation) { var type = _observationTypeRepository.GetById(observation.ObservationTypeId); //Email if (observation.EmailYesOrNo) { if (string.IsNullOrWhiteSpace(observation.Email)) { //Email validation is handled by the [EmailAddress] attribute on the domain validationDictionary.AddError("Email", Domain.Observation.Required); } } else { validationDictionary.Remove("Email"); observation.Email = null; } //Ensure type has a value first, may be a 0 / null type. if (type == null) { validationDictionary.AddError("ObservationTypeId", Domain.Observation.Required); } else { //Remove the built-in validation for observation category and other category //since we will override the rules to be more specific based on conditional //logic here. validationDictionary.Remove("ObservationCategoryId"); validationDictionary.Remove("OtherCategory"); if (type.Name == Domain.ObservationType.UnsafeCondition) { //A category is required if the observation type is "Unsafe Condition" if (!observation.ObservationCategoryId.HasValue) { validationDictionary.AddError("ObservationCategoryId", Domain.Observation.Required); } else { var category = _observationCategoryRepository.GetById(observation.ObservationCategoryId.Value); //If other specified, we need a reason (OtherCategory) if (category.Reference == Domain.ObservationCategory.Other && string.IsNullOrWhiteSpace(observation.OtherCategory)) { validationDictionary.AddError("OtherCategory", Domain.Observation.Required); } else { observation.OtherCategory = null; } } } else { //Remove category Id and other category if "Good Practice" is specified. observation.ObservationCategoryId = null; observation.OtherCategory = null; } } //Return whether the validation dictionary is valid. This is so that //we can extend the result given initially, for required etc. return(validationDictionary.IsValid); }
public static StringContent GetNotificationContent(this Domain.Observation observation, string observationId, string url, string producerId) { return(new StringContent(JsonConvert.SerializeObject(observation.GetNotificationData(observationId, url, null, producerId)), Encoding.UTF8, "application/json")); }