Advertisement
rj06

AddAgreement new code

Nov 24th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. public void AddAgreement(int personId, AgreementDto agreementDto)
  2.         {
  3.             using (var context = new GdDbContext() {Configuration = { AutoDetectChangesEnabled = false, LazyLoadingEnabled = false}})
  4.             {
  5.                 //context.Configuration.AutoDetectChangesEnabled = false;
  6.                 //context.Configuration.LazyLoadingEnabled = true;
  7.  
  8.                 //var personAdminId = Context.Users.Single(x => x.Email == ConfigurationManager.AppSettings["DefaultGdAdminEmail"]).PersonId;
  9.                 // Dit werkt niet. Moet in 2 stappen
  10.                 var adminEmail = ConfigurationManager.AppSettings["DefaultGdAdminEmail"];
  11.                 var personAdminId = context.Users.FirstOrDefault(x => x.Email == adminEmail).PersonId;
  12.                 var personPM = context.Persons.First(x => x.Name == "Permission Machine");
  13.                 var person = context.Persons.FirstOrDefault(el => el.Id == personId);
  14.  
  15.                 if (person == null)
  16.                 {
  17.                     throw new GraphicsDetectiveInvalidDataTypeException($"No person found for Id: {personId}");
  18.                 }
  19.  
  20.                 if (agreementDto == null)
  21.                 {
  22.                     throw new GraphicsDetectiveInvalidDataTypeException("Invalid agreementDto");
  23.                 }
  24.  
  25.                 var agreement = new Agreement
  26.                 {
  27.                     Date = agreementDto.DateTime,
  28.                     AgreementType = AgreementType.WwwImageSearch,
  29.                     ImageSearchAppointments = new List<ImageSearchAppointment>()
  30.                 };
  31.                 context.Agreements.Add(agreement);
  32.  
  33.                 var personAgreementRelations = new List<PersonAgreementRelation>()
  34.                 {
  35.                     new PersonAgreementRelation
  36.                     {
  37.                         Agreement = agreement,
  38.                         Person = person,
  39.                         PersonAgreementRole = PersonAgreementRole.Client
  40.                     },
  41.                     new PersonAgreementRelation
  42.                     {
  43.                         Agreement = agreement,
  44.                         PersonAgreementRole = PersonAgreementRole.Supplier,
  45.                         PersonId = personPM.Id
  46.                     },
  47.                     new PersonAgreementRelation
  48.                     {
  49.                         Agreement = agreement,
  50.                         PersonAgreementRole = PersonAgreementRole.Admin,
  51.                         PersonId = personAdminId
  52.                     }
  53.                 };
  54.                 context.PersonAgreementRelations.AddRange(personAgreementRelations);
  55.  
  56.                 //TODO: Check if OKAY!!!
  57.  
  58.                 if (agreementDto.ImageSearchAppointmentDto.Count == 0)
  59.                 {
  60.                     throw new GraphicsDetectiveInvalidDataTypeException(
  61.                         "Count of imagesearchappointments can't be lower than 0");
  62.                 }
  63.                 var imageSearchAppointmentDto = agreementDto.ImageSearchAppointmentDto.First();
  64.                 var imageSearchAppointment = new ImageSearchAppointment()
  65.                 {
  66.                     AgreementId = agreement.Id,
  67.                     HasImageFeed = false,
  68.                     Name = imageSearchAppointmentDto.Name,
  69.                     Periodicity = imageSearchAppointmentDto.Periodicity,
  70.                     PeriodicityCategory = imageSearchAppointmentDto.PeriodicityCategory,
  71.                     ShowResultsToCustomer = imageSearchAppointmentDto.ShowResultsToCustomer,
  72.                     ImageSearchCommands = new List<ImageSearchCommand>()
  73.                 };
  74.                 context.ImageSearchAppointments.Add(imageSearchAppointment);
  75.  
  76.                 if (!imageSearchAppointment.ShowResultsToCustomer)
  77.                 {
  78.                     var imageSearchAppointmentWebDomainExtensions = imageSearchAppointmentDto
  79.                         .WebDomainExtensionDtos
  80.                         .Select(x => new ImageSearchAppointmentWebDomainExtension()
  81.                         {
  82.                             WebDomainExtensionId = x.Id,
  83.                             ImageSearchAppointment = imageSearchAppointment,
  84.                             ImageSearchAppointmentId = imageSearchAppointment.Id
  85.                         })
  86.                         .ToList();
  87.  
  88.                     context.ImageSearchAppointmentWebDomainExtensions.AddRange(imageSearchAppointmentWebDomainExtensions);
  89.                 }
  90.  
  91.                 var imageSearchCommandDto = imageSearchAppointmentDto.ImageSearchCommandDto.First();
  92.                 var imageSearchCommand = new ImageSearchCommand()
  93.                 {
  94.                     ImageSearchAppointment = imageSearchAppointment,
  95.                     ImageSearchAppointmentId = imageSearchAppointment.Id,
  96.                     Date = imageSearchCommandDto.Date,
  97.                     NumberOfImages = imageSearchCommandDto.NumberOfImages,
  98.                     ImageCollectionProcessedDate = imageSearchCommandDto.ImageCollectionProcessedDate
  99.                 };
  100.                 context.ImageSearchCommands.Add(imageSearchCommand);
  101.                        
  102.                 context.ChangeTracker.DetectChanges();
  103.                 context.SaveChanges();
  104.                 context.Configuration.LazyLoadingEnabled = true;
  105.             }
  106.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement