Ejemplo n.º 1
0
        public IHttpActionResult PutLine(int id, Line line)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != line.Id)
            {
                return(BadRequest());
            }

            if (line.Stations == null || line.Stations.Count < 2)
            {
                return(Content(HttpStatusCode.BadRequest, "You must add at least two stations per line"));
            }
            //List<Line> stats = unitOfWork.Lines.GetAllLinesWithStations().ToList();
            Line lineDb = unitOfWork.Lines.Get(id);

            if (lineDb != null)
            {
                if (lineDb.Version > line.Version)
                {
                    return(Content(HttpStatusCode.Conflict, "CONFLICT You are trying to edit a Line that has been changed recently. Try again. "));
                }

                string poruka = unitOfWork.Lines.ReplaceStations(line.Id, line.Stations);
                if (poruka == "notOk")
                {
                    return(Content(HttpStatusCode.Conflict, $" You are trying to edit a Line which station has been changed. Try again."));
                }
                else if (poruka == "nullStation")
                {
                    return(Content(HttpStatusCode.Conflict, $" You are trying to edit a Line which station has been removed. Try again."));
                }


                List <SerialNumberSL> st = unitOfWork.SerialNumberSLs.GetAll().Where(sy => sy.LineId == line.Id).ToList();
                unitOfWork.SerialNumberSLs.RemoveRange(st);
                int i = 0;
                foreach (Station s in line.Stations)
                {
                    i++;
                    SerialNumberSL o = new SerialNumberSL();
                    o.LineId       = line.Id;
                    o.StationId    = s.Id;
                    o.SerialNumber = i;
                    unitOfWork.SerialNumberSLs.Add(o);
                }
            }
            else
            {
                return(Content(HttpStatusCode.NotFound, "Line that you are trying to edit either do not exist or was previously deleted by another user."));
            }

            lineDb.Version++;
            unitOfWork.Lines.Update(lineDb);

            try
            {
                unitOfWork.Complete();
                return(Ok(lineDb.Id));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Content(HttpStatusCode.Conflict, ex));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult PostLine(Line line)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (LineExists(line.Id))
            {
                return(Content(HttpStatusCode.Conflict, "CONFLICT Line already exists!"));
            }

            if (line.Version != 0)
            {
                line.Version = 0;
            }

            if (line.LineNumber == "" || line.LineNumber == null)
            {
                return(Content(HttpStatusCode.BadRequest, "You have to fill line number!"));
            }

            List <Line> listOfLines = unitOfWork.Lines.GetAll().ToList();

            if (listOfLines.Exists(x => x.LineNumber == line.LineNumber))
            {
                return(Content(HttpStatusCode.BadRequest, "Line with that number already exists!"));
            }
            if (line.Stations == null || line.Stations.Count < 2)
            {
                return(Content(HttpStatusCode.BadRequest, "You must add at least two stations per line"));
            }

            Line l = new Line();

            l.Stations   = new List <Station>();
            l.LineNumber = line.LineNumber;
            l.ColorLine  = line.ColorLine;

            List <Station> stats = unitOfWork.Stations.GetAll().ToList();
            int            i     = 0;

            foreach (Station s in line.Stations)
            {
                Station stationAdd = stats.Find(x => x.Id.Equals(s.Id));
                if (stationAdd == null)
                {
                    return(Content(HttpStatusCode.Conflict, "CONFLICT Station you want to add in line has been removed!"));
                }
                else
                {
                    if (stationAdd.Version > s.Version)
                    {
                        return(Content(HttpStatusCode.Conflict, "CONFLICT Station you want to add in line has been changed!"));
                    }
                }
                i++;
                SerialNumberSL o = new SerialNumberSL();
                o.LineId       = line.Id;
                o.StationId    = s.Id;
                o.SerialNumber = i;
                unitOfWork.SerialNumberSLs.Add(o);
                l.Stations.Add(stats.Find(x => x.Id.Equals(s.Id)));
            }

            try
            {
                unitOfWork.Lines.Add(l);
                unitOfWork.Complete();
                return(Ok(l.Id));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Content(HttpStatusCode.Conflict, ex));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }