public JsonResult GetHoses(int dispenserId)
        {
            DispenserEntity dispenser = _context.Dispensers
                                        .Include(d => d.Hoses)
                                        .FirstOrDefault(d => d.Id == dispenserId);

            if (dispenser == null)
            {
                return(null);
            }
            List <HoseEntity> aux = dispenser.Hoses.ToList();

            List <HoseEntity> disps = _context.MedTemps.Include(m => m.Hose).Select(t => new HoseEntity
            {
                Id     = t.Hose.Id,
                Number = t.Hose.Number,
                Type   = t.Hose.Type
            }).ToList();

            if (disps != null)
            {
                foreach (HoseEntity item in disps)
                {
                    HoseEntity test = _context.Hoses.Find(item.Id);
                    aux.Remove(test);
                }
            }
            JsonResult x = Json(aux.OrderBy(c => c.Number));

            return(Json(aux.OrderBy(h => h.Number)));
        }
        public async Task <IActionResult> AddHose(HoseEntity hose)
        {
            if (ModelState.IsValid)
            {
                DispenserEntity dispenser = await _context.Dispensers
                                            .Include(c => c.Hoses)
                                            .FirstOrDefaultAsync(c => c.Id == hose.IdDispenser);

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

                switch (hose.Type)
                {
                case "1":
                    hose.Type = TypeFuel.Regular.ToString();
                    break;

                case "0":
                    hose.Type = TypeFuel.Diesel.ToString();
                    break;

                case "2":
                    hose.Type = TypeFuel.Super.ToString();
                    break;

                case "3":
                    hose.Type = TypeFuel.Exonerado.ToString();
                    break;
                }

                try
                {
                    hose.Id = 0;
                    dispenser.Hoses.Add(hose);
                    _context.Update(dispenser);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction($"{nameof(DetailsDispenser)}/{dispenser.Id}"));
                }
                catch (DbUpdateException dbUpdateException)
                {
                    if (dbUpdateException.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "There are a record with the same name.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message);
                    }
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }

            return(View(hose));
        }
        public async Task <IActionResult> DeleteHose(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }



            HoseEntity hose = await _context.Hoses
                              .FirstOrDefaultAsync(m => m.Id == id);

            if (hose == null)
            {
                return(NotFound());
            }
            DispenserEntity disp = _context.Dispensers
                                   .Include(d => d.Hoses)
                                   .Where(d => d.Hoses.Contains(hose))
                                   .FirstOrDefault();

            _context.Hoses.Remove(hose);
            await _context.SaveChangesAsync();



            return(RedirectToAction($"{nameof(DetailsDispenser)}/{disp.Id}"));
        }
Beispiel #4
0
        public IEnumerable <SelectListItem> GetComboHoses(int IdDispenser)
        {
            List <SelectListItem> list      = new List <SelectListItem>();
            DispenserEntity       dispenser = _context.Dispensers.Include(h => h.Hoses)
                                              .FirstOrDefault(d => d.Id == IdDispenser);

            if (dispenser != null)
            {
                List <HoseEntity> aux   = dispenser.Hoses.ToList();
                List <HoseEntity> disps = _context.MedTemps.Include(m => m.Hose).Select(t => new HoseEntity
                {
                    Id     = t.Hose.Id,
                    Number = t.Hose.Number,
                    Type   = t.Hose.Type
                }).ToList();

                if (disps != null)
                {
                    foreach (HoseEntity item in disps)
                    {
                        aux.Remove(item);
                    }
                }

                list = dispenser.Hoses.Select(t => new SelectListItem
                {
                    Text  = t.Type,
                    Value = $"{t.Id}"
                })
                       .OrderBy(t => t.Text)
                       .ToList();
            }


            list.Insert(0, new SelectListItem
            {
                Text  = "[Select a Hose...]",
                Value = "0"
            });

            return(list);
        }
        public async Task <IActionResult> AddHose(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DispenserEntity dispenser = await _context.Dispensers.FindAsync(id);

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

            HoseEntity model = new HoseEntity {
                IdDispenser = dispenser.Id
            };

            return(View(model));
        }
        public async Task <IActionResult> AddDispenser(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            StationEntity station = await _context.Stations.FindAsync(id);

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

            DispenserEntity model = new DispenserEntity {
                IdStation = station.Id
            };

            return(View(model));
        }
        public async Task <IActionResult> DetailsDispenser(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            DispenserEntity dispenser = await _context.Dispensers.Include(s => s.Hoses)
                                        .FirstOrDefaultAsync(d => d.Id == id);

            StationEntity station = _context.Stations.Include(s => s.Dispensers).Where(s => s.Dispensers.Contains(dispenser)).FirstOrDefault();

            dispenser.IdStation = station.Id;

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

            return(View(dispenser));
        }
        public async Task <IActionResult> AddDispenser(DispenserEntity dispsenser)
        {
            if (ModelState.IsValid)
            {
                StationEntity station = await _context.Stations
                                        .Include(c => c.Dispensers)
                                        .FirstOrDefaultAsync(c => c.Id == dispsenser.IdStation);

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

                try
                {
                    dispsenser.Id = 0;
                    station.Dispensers.Add(dispsenser);
                    _context.Update(station);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction($"{nameof(Details)}/{station.Id}"));
                }
                catch (DbUpdateException dbUpdateException)
                {
                    if (dbUpdateException.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "There are a record with the same name.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message);
                    }
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }

            return(View(dispsenser));
        }
        public async Task <IActionResult> DeleteDispenser(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DispenserEntity dispenser = await _context.Dispensers
                                        .Include(d => d.Hoses)
                                        .FirstOrDefaultAsync(m => m.Id == id);

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

            _context.Dispensers.Remove(dispenser);
            await _context.SaveChangesAsync();

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