예제 #1
0
        public ReadUnit(IConfiguration configuration)
        {
            var connectionString = configuration.GetConnectionString(CONNECTIONSTRING_KEY);

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException("Connection string missing");
            }
            _connectionString = connectionString;
            _context          = new DeliveryServiceDbContext(configuration);
        }
        public IEnumerable <PointDTO> ListAll()
        {
            IList <PointDTO> points = new List <PointDTO>();

            using (var dbContext = new DeliveryServiceDbContext())
            {
                points = dbContext.Points.Select(p => new PointDTO()
                {
                    Id   = p.Id,
                    Name = p.Name
                }).ToList <PointDTO>();
            }

            return(points);
        }
        public void Delete(int pointId)
        {
            using (var context = new DeliveryServiceDbContext())
            {
                Point pointEntity = context.Points.Find(pointId);

                if (pointEntity != null)
                {
                    context.Entry <Point>(pointEntity).State = EntityState.Deleted;
                    context.SaveChanges();
                }
                else
                {
                    throw new ArgumentNullException("pointId", "The referenced point does not exist.");
                }
            }
        }
예제 #4
0
        public void Delete(int routeId)
        {
            using (var context = new DeliveryServiceDbContext())
            {
                Route routeEntity = context.Routes.Find(routeId);

                if (routeEntity != null)
                {
                    context.Entry <Route>(routeEntity).State = EntityState.Deleted;
                    context.SaveChanges();
                }
                else
                {
                    throw new ArgumentNullException("routeId", "The referenced point does not exist.");
                }
            }
        }
        public PointDTO Get(int pointId)
        {
            PointDTO point = null;

            using (var dbContext = new DeliveryServiceDbContext())
            {
                point = dbContext.Points
                        .Select(p => new PointDTO()
                {
                    Id   = p.Id,
                    Name = p.Name
                })
                        .Where(p => p.Id.Equals(pointId))
                        .SingleOrDefault <PointDTO>();
            }

            return(point);
        }
예제 #6
0
        public RouteDTO Save(RouteDTO route)
        {
            RouteDTO savedRoute = null;

            using (var context = new DeliveryServiceDbContext())
            {
                Point sourceEntity      = context.Points.Find(route.OriginId);
                Point destinationEntity = context.Points.Find(route.DestinationId);

                if (sourceEntity == null)
                {
                    throw new ApplicationException("Route source point does not exist.");
                }

                if (destinationEntity == null)
                {
                    throw new ApplicationException("Route destination point does not exist.");
                }

                Route routeEntity = new Route()
                {
                    Id          = route.Id,
                    Origin      = sourceEntity,
                    Destination = destinationEntity,
                    Cost        = route.Cost,
                    Minutes     = route.Minutes
                };

                if (routeEntity.Id == 0)
                {
                    context.Entry(routeEntity).State = EntityState.Added;
                }
                else
                {
                    context.Entry(routeEntity).State = EntityState.Modified;
                }

                context.SaveChanges();

                savedRoute = typeMapper.Map <Route, RouteDTO>(routeEntity);
            }

            return(savedRoute);
        }
        public PointDTO Save(PointDTO point)
        {
            Point pointEntity = typeMapper.Map <PointDTO, Point>(point);

            using (var context = new DeliveryServiceDbContext())
            {
                if (pointEntity.Id == 0)
                {
                    context.Entry(pointEntity).State = EntityState.Added;
                }
                else
                {
                    context.Entry(pointEntity).State = EntityState.Modified;
                }

                context.SaveChanges();
            }

            return(typeMapper.Map <Point, PointDTO>(pointEntity));
        }
예제 #8
0
        public IEnumerable <RouteDTO> ListAll()
        {
            IList <RouteDTO> routes = new List <RouteDTO>();

            using (var dbContext = new DeliveryServiceDbContext())
            {
                routes = dbContext.Routes
                         .Select(r => new RouteDTO()
                {
                    Id              = r.Id,
                    Cost            = r.Cost,
                    Minutes         = r.Minutes,
                    OriginId        = r.Origin.Id,
                    DestinationId   = r.Destination.Id,
                    OriginName      = r.Origin.Name,
                    DestinationName = r.Destination.Name
                })
                         .ToList <RouteDTO>();
            }

            return(routes);
        }
예제 #9
0
        public RouteDTO Get(int routeId)
        {
            RouteDTO route = null;

            using (var dbContext = new DeliveryServiceDbContext())
            {
                route = dbContext.Routes
                        .Select(r => new RouteDTO()
                {
                    Id              = r.Id,
                    Cost            = r.Cost,
                    OriginId        = r.Origin.Id,
                    DestinationId   = r.Destination.Id,
                    OriginName      = r.Origin.Name,
                    DestinationName = r.Destination.Name
                })
                        .Where(p => p.Id.Equals(routeId))
                        .SingleOrDefault <RouteDTO>();
            }

            return(route);
        }
예제 #10
0
 public ReadRepository(DeliveryServiceDbContext context)
 {
     _context = context;
 }
예제 #11
0
 public WriteRepository(DeliveryServiceDbContext context)
 {
     _context = context;
 }