Example #1
0
        public IHttpActionResult GetStationsByDrivelineNumber(int number)
        {
            Driveline driveline = unitOfWork.Drivelines.GetLineByNumber(number);

            if (driveline == null || driveline.Stations.Count == 0)
            {
                return(NotFound());
            }
            List <UpdateStationInfoBindingModel> stations = new List <UpdateStationInfoBindingModel>();

            foreach (Station s in driveline.Stations)
            {
                UpdateStationInfoBindingModel station = new UpdateStationInfoBindingModel();
                station.Id      = s.Id;
                station.Name    = s.Name;
                station.Address = s.Address;
                Coordinates cor = unitOfWork.CoordinatesRepository.Get(s.CoordinatesId);
                station.X = cor.CoordX;
                station.Y = cor.CoordY;

                stations.Add(station);
            }

            return(Ok(stations));
        }
        public UpdateStationInfoBindingModel GetStationsById(int id)
        {
            Station station = AppDbContext.Stations.Where(x => x.Id == id).FirstOrDefault();
            UpdateStationInfoBindingModel res;

            if (station != null)
            {
                res = new UpdateStationInfoBindingModel()
                {
                    Id = id
                };
                res.Name    = station.Name;
                res.Address = station.Address;
                Coordinates co = AppDbContext.Coordinates.Where(c => c.CoordinatesId == station.CoordinatesId).FirstOrDefault();
                res.X          = co.CoordX;
                res.Y          = co.CoordY;
                res.RowVersion = station.RowVersion;

                return(res);
            }



            return(null);
        }
        public IHttpActionResult GetStationsById(int id)
        {
            UpdateStationInfoBindingModel station = unitOfWork.Stations.GetStationsById(id);

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

            return(Ok(station));
        }
        public HttpStatusCode UpdateStationInfo(UpdateStationInfoBindingModel bindingModel)
        {
            try
            {
                Station station = AppDbContext.Stations.Where(x => x.Id == bindingModel.Id).FirstOrDefault();
                if (station != null)
                {
                    Station s = AppDbContext.Stations.Where(x => x.Name == bindingModel.Name && x.Id != bindingModel.Id).FirstOrDefault();
                    if (s != null) //ako postoji stanica sa takvim imenom, vrati gresku
                    {
                        return(HttpStatusCode.BadRequest);
                    }

                    for (int i = 0; i < bindingModel.RowVersion.Count(); i++)
                    {
                        if (bindingModel.RowVersion[i] != station.RowVersion[i])
                        {
                            return(HttpStatusCode.Conflict);
                        }
                    }

                    station.Name    = bindingModel.Name;
                    station.Address = bindingModel.Address;

                    Coordinates co = new Coordinates()
                    {
                        CoordX = bindingModel.X, CoordY = bindingModel.Y
                    };
                    AppDbContext.Coordinates.Add(co);
                    AppDbContext.SaveChanges();

                    int corId = AppDbContext.Coordinates.Where(x => x.CoordX == co.CoordX && x.CoordY == co.CoordY).FirstOrDefault().CoordinatesId;
                    station.CoordinatesId = corId;

                    AppDbContext.SaveChanges();

                    return(HttpStatusCode.OK);
                }
                return(HttpStatusCode.NotFound);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Trace.WriteLine("DbUpdateConcurrencyException Message: {0}", ex.Message);
                return(HttpStatusCode.Conflict);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("NormalException Message: {0}", ex.Message);
                return(HttpStatusCode.Conflict);
            }
        }
        public IHttpActionResult UpdateStationInfo(UpdateStationInfoBindingModel bindingModel)
        {
            HttpStatusCode response = unitOfWork.Stations.UpdateStationInfo(bindingModel);

            if (response == HttpStatusCode.OK)
            {
                return(Ok());
            }
            if (response == HttpStatusCode.BadRequest)
            {
                return(BadRequest("There is allready a station with this name, station names must be uniqe"));
            }
            if (response == HttpStatusCode.Conflict)
            {
                return(Conflict());
            }

            return(NotFound());
        }
        public List <UpdateStationInfoBindingModel> GetAllStations()
        {
            List <UpdateStationInfoBindingModel> stations = new List <UpdateStationInfoBindingModel>();
            List <Station> dbStations = AppDbContext.Stations.ToList();

            foreach (Station s in dbStations)
            {
                UpdateStationInfoBindingModel station = new UpdateStationInfoBindingModel()
                {
                    Id = s.Id, Name = s.Name, Address = s.Address
                };
                Coordinates cor = AppDbContext.Coordinates.Where(x => x.CoordinatesId == s.CoordinatesId).FirstOrDefault();

                station.X = cor.CoordX;
                station.Y = cor.CoordY;

                stations.Add(station);
            }

            return(stations);
        }