예제 #1
0
        public async Task <string> Insert(Location location)
        {
            if (context.Locations.Any(x => x.LocationId == location.LocationId))
            {
                return(null);
            }
            await context.Locations.AddAsync(location);

            await context.SaveChangesAsync();

            return(location.LocationId);
        }
예제 #2
0
        public async Task Update(string locationId, IEnumerable <ChargePoint> chargePoints)
        {
            var requestIds = chargePoints.Select(x => x.ChargePointId);

            var existingIds = context.ChargePoints
                              .Where(x => x.LocationId == locationId)
                              .Select(x => x.ChargePointId)
                              .ToList();
            var newChargePoints    = chargePoints.Where(x => !existingIds.Contains(x.ChargePointId));
            var newChargePointsIds = newChargePoints.Select(x => x.ChargePointId);

            requestIds = requestIds.Where(x => !newChargePointsIds.Contains(x));

            var idsToDelete = existingIds.Where(x => !requestIds.Contains(x));

            foreach (var id in idsToDelete)
            {
                var chargePoint = new ChargePoint()
                {
                    ChargePointId = id, Status = "Deleted"
                };
                context.Attach(chargePoint);
                context.Entry(chargePoint).Property(x => x.Status).IsModified = true;
            }

            foreach (var chargePoint in chargePoints.Where(x => !idsToDelete.Contains(x.ChargePointId) &&
                                                           !newChargePointsIds.Contains(x.ChargePointId)))
            {
                context.Entry(chargePoint).State = EntityState.Modified;
            }

            await context.AddRangeAsync(newChargePoints);

            await context.SaveChangesAsync();
        }