Exemplo n.º 1
0
        public async Task <ActionResult> AddStation(FillingStationDTO fillingStationDTO)
        {
            var similarManagerEmail = await DbContext.FillingStations.AnyAsync(i => i.ManagerEmail.ToLower() == fillingStationDTO.ManagerEmail.ToLower());

            if (similarManagerEmail)
            {
                return(BadRequest($"The manager with email {fillingStationDTO.ManagerEmail} is already assigned a different station"));
            }


            var fillingStation = Mapper.Map <FillingStation>(fillingStationDTO);


            await DbContext.FillingStations.AddAsync(fillingStation);

            await DbContext.SaveChangesAsync();

            foreach (var pump in fillingStation.Pumps)
            {
                foreach (var nozzle in pump.Nozzles)
                {
                    var productTankForStation = DbContext.Tanks.FirstOrDefault(i => i.ProductId == nozzle.ProductId && i.StationId == pump.StationId);
                    if (productTankForStation == null)
                    {
                        try
                        {
                            var productName = DbContext.Products.Find(nozzle.ProductId).ProductName;
                            DbContext.Tanks.Add(new Tank
                            {
                                StationId = pump.StationId,
                                ProductId = nozzle.ProductId,
                                TankName  = productName + " Tank"
                            });
                        }
                        catch (Exception ex) {  }
                    }
                    DbContext.SaveChanges();
                }
            }



            return(Ok(fillingStation.FillingStationId));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> EditStation(FillingStationDTO fillingStationDTO)
        {
            var fillingStation = Mapper.Map <FillingStation>(fillingStationDTO);

            var stationFromDB = await DbContext.FillingStations.FindAsync(fillingStationDTO.FillingStationId);

            stationFromDB.Name         = fillingStationDTO.Name;
            stationFromDB.Location     = fillingStationDTO.Location;
            stationFromDB.ManagerEmail = fillingStationDTO.ManagerEmail;

            await DbContext.SaveChangesAsync();

            foreach (var pump in fillingStation.Pumps)
            {
                try
                {
                    var _pump = await DbContext.Pumps.FindAsync(pump.PumpId);

                    if (_pump == null)
                    {
                        await DbContext.Pumps.AddAsync(pump);
                    }
                    else
                    {
                        _pump.PumpName      = pump.PumpName;
                        _pump.AttendantName = pump.AttendantName;
                    }
                }
                catch { }

                foreach (var nozzle in pump.Nozzles)
                {
                    var _nozzle = await DbContext.Nozzles.FindAsync(nozzle.NozzleId);

                    if (_nozzle == null)
                    {
                        await DbContext.Nozzles.AddAsync(nozzle);
                    }
                    else
                    {
                        _nozzle.NozzleName = nozzle.NozzleName;
                        _nozzle.ProductId  = nozzle.ProductId;
                    }

                    var productTankForStation = DbContext.Tanks.FirstOrDefault(i => i.ProductId == nozzle.ProductId && i.StationId == pump.StationId);
                    if (productTankForStation == null)
                    {
                        try
                        {
                            var productName = DbContext.Products.Find(nozzle.ProductId).ProductName;
                            DbContext.Tanks.Add(new Tank
                            {
                                StationId = pump.StationId,
                                ProductId = nozzle.ProductId,
                                TankName  = productName + " Tank"
                            });
                        }
                        catch (Exception ex) { }
                    }

                    //}
                    //catch { }
                    DbContext.SaveChanges();
                }
            }



            DbContext.SaveChanges();
            return(Ok());
        }