Exemplo n.º 1
0
        public static List <CourtWorkDetail> GetCourtWorkDetails(List <QuotedWork> quotedWorks)
        {
            var mainAreas = quotedWorks.GroupBy(work => work.QuotedWorkMainAreaName);
            List <CourtWorkDetail> courtWorkDetails = new List <CourtWorkDetail>();

            foreach (var mainArea in mainAreas)
            {
                var        worksInArea = quotedWorks.Where(work => work.QuotedWorkMainAreaName.Equals(mainArea.Key));
                QuotedWork quotedWork  = worksInArea.FirstOrDefault();
                if (quotedWork == null)
                {
                    throw new Exception("Something went horribly wrong");
                }
                CourtWorkDetail details = new CourtWorkDetail()
                {
                    AreaName       = quotedWork.QuotedWorkMainAreaName,
                    NumberOfCourts = quotedWork.NumberOfCourts,
                    Works          =
                        worksInArea.Select(
                            workItem =>
                            new WorkItem()
                    {
                        Description = workItem.QuotedWorkDescription,
                        WorkArea    = workItem.QuotedWorkSubAreaName,
                        Price       = workItem.QuotedWorkPrice
                    }).ToList()
                };
                courtWorkDetails.Add(details);
            }
            return(courtWorkDetails);
        }
Exemplo n.º 2
0
        public static void CreateQuote(string quoteId, int locationId, int contactId, string quoteDate,
                                       List <CourtWorkDetail> works)
        {
            DateTime date  = DateTime.ParseExact(quoteDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
            Quote    quote = new Quote {
                ContactId = contactId, WorkLocationId = locationId, QuoteDate = date, QuoteId = quoteId, Archived = false, Finished = false
            };

            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                context.Quotes.Add(quote);
                foreach (CourtWorkDetail quoteWorkDetail in works)
                {
                    foreach (WorkItem workItem in quoteWorkDetail.Works)
                    {
                        QuotedWork quotedWork = new QuotedWork()
                        {
                            Quote = quote,
                            QuotedWorkMainAreaName = quoteWorkDetail.AreaName,
                            QuotedWorkDescription  = workItem.Description,
                            QuotedWorkSubAreaName  = string.IsNullOrEmpty(workItem.WorkArea) ? string.Empty : workItem.WorkArea,
                            WorkTitle       = workItem.WorkTitle,
                            NumberOfCourts  = quoteWorkDetail.NumberOfCourts,
                            QuotedWorkPrice = workItem.Price
                        };
                        context.QuotedWorks.Add(quotedWork);
                    }
                }
                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }
        }