Пример #1
0
        public async Task <IActionResult> DeleteCombinedLink([FromRoute] string id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var customerLink = await _linkContext.CustomerLink.FindAsync(id);

            if (customerLink == null)
            {
                return(NotFound());
            }

            CombinedLink combinedLink = GetFullLinkFromContexts(customerLink);

            if (combinedLink.NewC != null)
            {
                _newContext.NewCustomer.Remove(combinedLink.NewC);
                await _newContext.SaveChangesAsync();
            }

            if (combinedLink.OldC != null)
            {
                _oldContext.OldCustomer.Remove(combinedLink.OldC);
                await _oldContext.SaveChangesAsync();
            }

            _linkContext.CustomerLink.Remove(customerLink);
            await _linkContext.SaveChangesAsync();

            return(Ok(customerLink));
        }
Пример #2
0
        public async Task <IActionResult> PutCombinedLink([FromRoute] string id, [FromBody] CombinedLink combinedLink)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != combinedLink.Link.ID)
            {
                return(BadRequest());
            }

            _linkContext.Entry(combinedLink.Link).State = EntityState.Modified;

            if (combinedLink.Link.NewID != "" && combinedLink.Link.OldID != -1)
            {
                // We want to remove the other CustomerLink that was combined.
                // First search for a link with the same ID for the customer's new data
                IEnumerable <CustomerLink> filteredContext =
                    _linkContext.CustomerLink.Where(x => (x.NewID == combinedLink.Link.NewID ||
                                                          x.OldID == combinedLink.Link.OldID));
                if (filteredContext.Count() > 1)
                {
                    List <CustomerLink> toRemove = new List <CustomerLink>();
                    foreach (CustomerLink linkedData in filteredContext)
                    {
                        if (linkedData.ID != combinedLink.Link.ID)
                        {
                            toRemove.Add(linkedData);
                        }
                    }

                    foreach (CustomerLink linkToRemove in toRemove)
                    {
                        _linkContext.CustomerLink.Remove(linkToRemove);
                    }
                }
            }

            try
            {
                await _linkContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerLinkExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> GetCombinedLink([FromRoute] string id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var customerLink = await _linkContext.CustomerLink.FindAsync(id);

            if (customerLink == null)
            {
                return(NotFound());
            }

            CombinedLink fullData = GetFullLinkFromContexts(customerLink);

            return(Ok(fullData));
        }
Пример #4
0
        /* This method packages the data from the different contexts into one combined object */
        private CombinedLink GetFullLinkFromContexts(CustomerLink bareCustomerLink)
        {
            NewCustomer newCustomerData = null;

            using (NewCustomersContext newContext = new NewCustomersContext(newCOptions))
            {
                if (bareCustomerLink.NewID != "")
                {
                    IQueryable <NewCustomer> filteredContext =
                        newContext.NewCustomer.Where(x => x.Id == bareCustomerLink.NewID);
                    if (filteredContext.Count() == 1)
                    {
                        newCustomerData = filteredContext.Single();
                    }
                }
            }

            OldCustomer oldCustomerData = null;

            using (OldCustomersContext oldContext = new OldCustomersContext(oldCOptions))
            {
                if (bareCustomerLink.OldID != -1)
                {
                    IQueryable <OldCustomer> filteredContext =
                        oldContext.OldCustomer.Where(x => x.Id == bareCustomerLink.OldID);
                    if (filteredContext.Count() == 1)
                    {
                        oldCustomerData = filteredContext.Single();
                    }
                }
            }

            CombinedLink fullLink = new CombinedLink
            {
                Link = bareCustomerLink,
                OldC = oldCustomerData,
                NewC = newCustomerData
            };

            return(fullLink);
        }
Пример #5
0
        public async Task <IActionResult> PostCombinedLink([FromBody] CombinedLink combinedLink)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (combinedLink.NewC != null)
            {
                combinedLink.NewC.Id    = Guid.NewGuid().ToString();
                combinedLink.Link.NewID = combinedLink.NewC.Id;
                _newContext.NewCustomer.Add(combinedLink.NewC);
                await _newContext.SaveChangesAsync();
            }

            if (combinedLink.OldC != null)
            {
                combinedLink.OldC.Id    = _oldContext.OldCustomer.Count() + 1;
                combinedLink.Link.OldID = combinedLink.OldC.Id;
                _oldContext.OldCustomer.Add(combinedLink.OldC);
                await _oldContext.SaveChangesAsync();
            }

            System.Diagnostics.Debug.WriteLine("Adding combinedLink with " +
                                               combinedLink.Link.ID == null ? "null" : combinedLink.Link.ID.ToString());
            System.Console.WriteLine("Adding combinedLink with " +
                                     combinedLink.Link.ID == null ? "null" : combinedLink.Link.ID.ToString());

            combinedLink.Link.ID = Guid.NewGuid().ToString();
            _linkContext.CustomerLink.Add(combinedLink.Link);
            await _linkContext.SaveChangesAsync();

            System.Diagnostics.Debug.WriteLine("combinedLink ID is now " +
                                               combinedLink.Link.ID == null ? "null" : combinedLink.Link.ID.ToString());
            System.Console.WriteLine("combinedLink ID is now " +
                                     combinedLink.Link.ID == null ? "null" : combinedLink.Link.ID.ToString());

            return(Ok($"Customer with id {combinedLink.Link.ID} has successfully been mde"));
        }
Пример #6
0
        private CombinedLink GetFullLinkFromContexts(CustomerLink bareCustomerLink)
        {
            NewCustomer newCustomerData = null;

            if (bareCustomerLink.NewID != "")
            {
                IQueryable <NewCustomer> filteredContext =
                    _newContext.NewCustomer.Where(x => x.Id == bareCustomerLink.NewID);
                if (filteredContext.Count() == 1)
                {
                    newCustomerData = filteredContext.Single();
                }
            }

            OldCustomer oldCustomerData = null;

            if (bareCustomerLink.OldID != -1)
            {
                IQueryable <OldCustomer> filteredContext =
                    _oldContext.OldCustomer.Where(x => x.Id == bareCustomerLink.OldID);
                if (filteredContext.Count() == 1)
                {
                    oldCustomerData = filteredContext.Single();
                }
            }

            System.Diagnostics.Debug.WriteLine("Id: " /*+ oldCustomerData.Id*/);

            CombinedLink fullLink = new CombinedLink
            {
                Link = bareCustomerLink,
                OldC = oldCustomerData,
                NewC = newCustomerData
            };

            return(fullLink);
        }
Пример #7
0
        public void TestRegularGetCorrectResult()
        {
            using (var linksContext = new LinksContext(linksOptions))
            {
                CustomerLink customerLink0  = linksContext.CustomerLink.Where(x => x.NewID == newIDs[0]).Single();
                CustomerLink customerLink1a = linksContext.CustomerLink.Where(x => x.OldID == oldIDs[1]).Single();
                CustomerLink customerLink1b = linksContext.CustomerLink.Where(x => x.NewID == newIDs[1]).Single();

                CombinedLink customerData0  = GetFullLinkFromContexts(customerLink0);
                CombinedLink customerData1a = GetFullLinkFromContexts(customerLink1a);
                CombinedLink customerData1b = GetFullLinkFromContexts(customerLink1b);

                NewCustomersContext        newContext      = new NewCustomersContext(newCOptions);
                OldCustomersContext        oldContext      = new OldCustomersContext(oldCOptions);
                CustomerLinksController    linksController = new CustomerLinksController(linksContext, newContext, oldContext);
                IEnumerable <CombinedLink> customers       = linksController.GetCombinedLink();

                newContext.Dispose();
                oldContext.Dispose();

                bool[] foundCustomer = new bool[3];
                foundCustomer[0] = false;
                foundCustomer[1] = false;
                foundCustomer[2] = false;

                System.Console.WriteLine("Looking for customers...");
                System.Diagnostics.Debug.WriteLine("Looking for customers...");

                foreach (CombinedLink customer in customers)
                {
                    System.Diagnostics.Debug.WriteLine("Customer found:");
                    System.Console.WriteLine("Customer found");

                    if (customer.Equals(customerData0))
                    {
                        foundCustomer[0] = true;
                    }
                    else if (customer.Equals(customerData1a))
                    {
                        foundCustomer[1] = true;
                    }
                    else if (customer.Equals(customerData1b))
                    {
                        foundCustomer[2] = true;
                    }
                }

                if (!foundCustomer[0])
                {
                    Assert.Fail("There was no data for Elon Musk");
                }

                if (!foundCustomer[1])
                {
                    Assert.Fail("There was no old data for Jimmy Wales");
                }

                if (!foundCustomer[2])
                {
                    Assert.Fail("There was no new data for Jimmy Wales");
                }

                if (customers.Count() != 3)
                {
                    Assert.Fail("There were more than 3 customer links");
                }
            };
        }