public async Task <IActionResult> Create([Bind("ID,Shape,Quantity,Location")] BarGraph barGraph)
        {
            if (ModelState.IsValid)
            {
                _context.Add(barGraph);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(barGraph));
        }
Example #2
0
        public async Task <IActionResult> Create([Bind("ID,UserID,ReportID")] Favorite favorite)
        {
            if (ModelState.IsValid)
            {
                _context.Add(favorite);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(favorite));
        }
Example #3
0
        public async Task <IActionResult> Create([Bind("ID,Name,MostCommonShape,Sightings")] Location location)
        {
            if (ModelState.IsValid)
            {
                _context.Add(location);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(location));
        }
Example #4
0
        public async Task <IActionResult> Create([Bind("Location,Shape,Duration,Description,DateOccurred,DateSubmitted")] Report report)
        {
            if (ModelState.IsValid)
            {
                report.DatePosted = DateTime.Today;
                _context.Add(report);

                // add to location database
                Location l = _context.Locations.Where(lo => lo.Name.Equals(report.Location)).FirstOrDefault();

                if (l == null)
                {
                    l = new Location
                    {
                        Name            = report.Location,
                        MostCommonShape = report.Shape,
                        Sightings       = 1
                    };
                    BarGraph b = new BarGraph
                    {
                        Shape    = report.Shape,
                        Location = report.Location,
                        Quantity = 1
                    };

                    _context.Locations.Add(l);
                    _context.BarGraphs.Add(b);
                }
                else // location exists so update bargraph and most common shape
                {
                    // update location sightings
                    l.Sightings++;

                    BarGraph bargraph = _context.BarGraphs.Where(b => b.Location.Equals(l.Name) && b.Shape.Equals(report.Shape)).FirstOrDefault();

                    if (bargraph == null)
                    {
                        BarGraph b = new BarGraph
                        {
                            Shape    = report.Shape,
                            Location = report.Location,
                            Quantity = 1
                        };

                        l.MostCommonShape = report.Shape;

                        _context.BarGraphs.Add(b);
                    }
                    else
                    {
                        // update the correct bargraphs quantity
                        bargraph.Quantity++;

                        // update the most common shape in location
                        var bargraphs = await _context.BarGraphs.ToListAsync();

                        int max = 0;
                        foreach (BarGraph b in bargraphs)
                        {
                            if (max < b.Quantity)
                            {
                                l.MostCommonShape = b.Shape;
                            }
                        }
                    }
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(report));
        }