/// <summary> /// Database definitions /// </summary> /// <param name="modelBuilder">model builder</param> protected new virtual void OnModelCreating(ModelBuilder modelBuilder) { Cluster.OnModelCreating(modelBuilder); Contract.OnModelCreating(modelBuilder); Invoice.OnModelCreating(modelBuilder); Organisation.OnModelCreating(modelBuilder); OrganisationMetadata.OnModelCreating(modelBuilder); Server.OnModelCreating(modelBuilder); MicroService.OnModelCreating(modelBuilder); }
/// <summary> /// Update an existing organisation /// </summary> /// <param name="organisation">DTO containing the organisation definition</param> /// <param name="scope">In which scope is the organisation updated</param> /// <returns>The updated organisation</returns> public static Organisation Update(Monosoft.Auth.DTO.Organisation organisation, string scope) { // TODO: revisionslog, hvem har opdateret (fra hvad, til hvad og hvornår) Organisation dborg = DataContext.Instance.Organisations.Where(p => p.Id == organisation.Id).FirstOrDefault(); if (dborg != null) { dborg.Name = organisation.Name; dborg.ParentOrganisation = DataContext.Instance.Organisations.Where(p => p.Id == organisation.ParentOrganisation).FirstOrDefault(); dborg.Cluster = DataContext.Instance.Clusters.Where(p => p.ClusterId == organisation.Cluster.ClusterId).FirstOrDefault(); if (dborg.Cluster != null) { if (organisation.Contracts != null) { var removedContracts = dborg.Contracts.Where(p => organisation.Contracts.Select(x => x.ContractNo).Contains(p.ContractNo) == false).ToList(); var updatedContracts = dborg.Contracts.Where(p => organisation.Contracts.Select(x => x.ContractNo).Contains(p.ContractNo) == true).ToList(); var addedContracts = organisation.Contracts.Where(x => dborg.Contracts.Select(p => p.ContractNo).Contains(x.ContractNo) == false).ToList(); foreach (var contract in removedContracts) { DataContext.Instance.Contracts.Remove(contract); } foreach (var contract in addedContracts) { Contract dbcontract = new Contract(); dbcontract.Base64File = contract.Base64File; dbcontract.Description = contract.Description; dbcontract.Interval = contract.Interval; dbcontract.IsActive = contract.IsActive; dbcontract.NextInvoicedate = contract.NextInvoicedate; dbcontract.OngoingPayment = contract.OngoingPayment; dbcontract.Organisation = dborg; dbcontract.Payout = contract.Payout; dbcontract.Title = contract.Title; DataContext.Instance.Contracts.Add(dbcontract); } foreach (var contract in updatedContracts) { var dbcontract = DataContext.Instance.Contracts.Where(p => p.ContractNo == contract.ContractNo && p.Organisation == dborg).FirstOrDefault(); dbcontract.Base64File = contract.Base64File; dbcontract.Description = contract.Description; dbcontract.Interval = contract.Interval; dbcontract.IsActive = contract.IsActive; dbcontract.NextInvoicedate = contract.NextInvoicedate; dbcontract.OngoingPayment = contract.OngoingPayment; dbcontract.Organisation = dborg; dbcontract.Payout = contract.Payout; dbcontract.Title = contract.Title; } } if (organisation.Invoices != null) { var removedInvoices = dborg.Invoices.Where(p => organisation.Invoices.Select(x => x.Invoiceno).Contains(p.InvoiceNo) == false).ToList(); var updatedInvoices = dborg.Invoices.Where(p => organisation.Invoices.Select(x => x.Invoiceno).Contains(p.InvoiceNo) == true).ToList(); var addedInvoices = organisation.Invoices.Where(x => dborg.Invoices.Select(p => p.InvoiceNo).Contains(x.Invoiceno) == false).ToList(); foreach (var invoice in removedInvoices) { DataContext.Instance.Invoices.Remove(invoice); } foreach (var invoice in addedInvoices) { Invoice dbinvoice = new Invoice(); dbinvoice.Amout = invoice.Amout; dbinvoice.Base64File = invoice.Base64File; dbinvoice.Details = invoice.Details; dbinvoice.DueDate = invoice.DueDate; dbinvoice.Invoicedate = invoice.Invoicedate; dbinvoice.Organisation = dborg; dbinvoice.Status = invoice.Status; dbinvoice.Titel = invoice.Titel; DataContext.Instance.Invoices.Add(dbinvoice); } foreach (var invoice in updatedInvoices) { var dbinvoice = DataContext.Instance.Invoices.Where(p => p.InvoiceNo == invoice.InvoiceNo && p.Organisation == dborg).FirstOrDefault(); dbinvoice.Amout = invoice.Amout; dbinvoice.Base64File = invoice.Base64File; dbinvoice.Details = invoice.Details; dbinvoice.DueDate = invoice.DueDate; dbinvoice.Invoicedate = invoice.Invoicedate; dbinvoice.Organisation = dborg; dbinvoice.Status = invoice.Status; dbinvoice.Titel = invoice.Titel; } } dborg.Metadata.Clear(); foreach (var metadata in organisation.Metadata) { OrganisationMetadata meta = new OrganisationMetadata(); meta.Organisation = dborg; meta.Key = metadata.Key; // if (metadata.Scope != scope) // { // throw new Exception("CRITICAL ERROR: Metadata scope does not match login scope"); // } meta.Scope = metadata.Scope; meta.Value = metadata.Value; DataContext.Instance.OrganisationMetadatas.Add(meta); } DataContext.Instance.SaveChanges(); return(dborg); } } return(null); }
/// <summary> /// Create a new organisation /// </summary> /// <param name="organisation">DTO containing the organisation definition</param> /// <param name="scope">In which scope is the organisation created</param> /// <returns>The created organisation</returns> public static Organisation Create(Monosoft.Auth.DTO.Organisation organisation, string scope) { // TODO: revisionslog, hvem har opdateret (fra hvad, til hvad og hvornår) Organisation dborg = new Organisation(); dborg.Name = organisation.Name; dborg.ParentOrganisation = DataContext.Instance.Organisations.Where(p => p.Id == organisation.ParentOrganisation).FirstOrDefault(); dborg.Cluster = DataContext.Instance.Clusters.Where(p => p.ClusterId == organisation.Cluster.ClusterId).FirstOrDefault(); if (dborg.Cluster != null) // dborg.ParentOrganisation != null && <-- fjernet pga. problem med første org. (i.e. vil altid være null) { DataContext.Instance.Organisations.Add(dborg); if (organisation.Contracts != null) { foreach (var contract in organisation.Contracts) { Contract dbcontract = new Contract(); dbcontract.Organisation = dborg; dbcontract.Base64File = contract.Base64File; dbcontract.Description = contract.Description; dbcontract.Interval = contract.Interval; dbcontract.IsActive = contract.IsActive; dbcontract.NextInvoicedate = contract.NextInvoicedate; dbcontract.OngoingPayment = contract.OngoingPayment; dbcontract.Payout = contract.Payout; dbcontract.Title = contract.Title; DataContext.Instance.Contracts.Add(dbcontract); } } if (organisation.Invoices != null) { foreach (var invoice in organisation.Invoices) { Invoice dbinvoice = new Invoice(); dbinvoice.Organisation = dborg; dbinvoice.Amout = invoice.Amout; dbinvoice.Base64File = invoice.Base64File; dbinvoice.Details = invoice.Details; dbinvoice.DueDate = invoice.DueDate; dbinvoice.Invoicedate = invoice.Invoicedate; dbinvoice.Status = invoice.Status; dbinvoice.Titel = invoice.Titel; DataContext.Instance.Invoices.Add(dbinvoice); } } if (organisation.Metadata != null) { foreach (var metadata in organisation.Metadata) { OrganisationMetadata meta = new OrganisationMetadata(); meta.Organisation = dborg; meta.Key = metadata.Key; // if (metadata.Scope != scope) //{ // throw new Exception("CRITICAL ERROR: Metadata scope does not match login scope"); //} meta.Scope = metadata.Scope; meta.Value = metadata.Value; DataContext.Instance.OrganisationMetadatas.Add(meta); } } DataContext.Instance.SaveChanges(); return(dborg); } return(null); }