Advertisement
rj06

AddAgreement method

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