예제 #1
0
        public void Should_generate_suggestions_from_examples_1()
        {
            using (var context = new ApplicationDbContext(ContextOptions))
            {
                List <Connector> connectors = new List <Connector>();
                connectors.Add(new Connector(1, 10));
                connectors.Add(new Connector(2, 10));
                connectors.Add(new Connector(3, 10));
                connectors.Add(new Connector(4, 10));
                connectors.Add(new Connector(5, 10));
                connectors.Add(new Connector(6, 20));
                connectors.Add(new Connector(7, 20));


                RemoveSuggestions removeSuggestion = new RemoveSuggestions();
                Assert.DoesNotThrow(() => removeSuggestion.GenerateAllSuggestions(connectors, 5f));

                Assert.AreEqual(5, removeSuggestion.Count);
                Assert.AreEqual(1, removeSuggestion[0].Count);
                Assert.AreEqual(1, removeSuggestion[1].Count);
                Assert.AreEqual(1, removeSuggestion[2].Count);
                Assert.AreEqual(1, removeSuggestion[3].Count);
                Assert.AreEqual(1, removeSuggestion[4].Count);
            }
        }
 public CapacityExceededProblemDetail(float exceededCapacity, RemoveSuggestions removeSuggestions)
 {
     Title             = "Capacity exceeded";
     Status            = 400;
     Type              = typeof(CapacityExceededProblemDetail).ToString();
     Detail            = $"The group capacity has exceeded by {exceededCapacity}A, remove the suggested connectors to free up space";
     ExceededCapacity  = exceededCapacity;
     RemoveSuggestions = removeSuggestions;
 }
예제 #3
0
        public void Should_generate_suggestions_with_exact_amount()
        {
            using (var context = new ApplicationDbContext(ContextOptions))
            {
                List <Connector> connectors = new List <Connector>();
                connectors.Add(new Connector(1, 20));
                connectors.Add(new Connector(2, 30));
                connectors.Add(new Connector(3, 30));
                connectors.Add(new Connector(4, 30));


                RemoveSuggestions removeSuggestion = new RemoveSuggestions();
                Assert.DoesNotThrow(() => removeSuggestion.GenerateAllSuggestions(connectors, 20f));

                Assert.AreEqual(1, removeSuggestion.Count);
                Assert.AreEqual(1, removeSuggestion[0].Count);
            }
        }
        public async Task <ActionResult <ConnectorResource> > Patch(Guid groupId, Guid chargeStationId, int connectorId, [FromBody] PatchConnectorResource patchConnectorResource)
        {
            Group group = await _context.Group.Include(g => g.ChargeStations).ThenInclude(c => c.Connectors).FirstOrDefaultAsync(g => g.Id == groupId);

            if (group == null)
            {
                return(NotFound("Group not found"));
            }

            ChargeStation chargeStation;

            if (!group.ChargeStations.TryGetValue(new ChargeStation(chargeStationId), out chargeStation))
            {
                return(NotFound("Charge station not found"));
            }

            Connector connector;

            if (!chargeStation.Connectors.TryGetValue(new Connector(connectorId), out connector))
            {
                return(NotFound("Connector not found"));
            }

            if (patchConnectorResource.MaxCurrentAmp.HasValue)
            {
                if (patchConnectorResource.MaxCurrentAmp.Value > connector.MaxCurrentAmp && group.HasExceededCapacity(patchConnectorResource.MaxCurrentAmp.Value - connector.MaxCurrentAmp))
                {
                    List <Connector> connectors        = (List <Connector>)_context.Connector.Where(c => c.ChargeStation.GroupId == groupId).OrderBy(o => o.MaxCurrentAmp);
                    float            excdeededCapacity = group.GetExceededCapacity();

                    RemoveSuggestions removeSuggestions = new RemoveSuggestions();
                    removeSuggestions.GenerateAllSuggestions(connectors, excdeededCapacity);
                    throw new CapacityExceededException(excdeededCapacity, removeSuggestions);
                }
                connector.ChangeMaxCurrentAmp(patchConnectorResource.MaxCurrentAmp.Value);
            }

            await _context.SaveChangesAsync();

            ConnectorResource connectorUpdated = _mapper.Map <ConnectorResource>(connector);

            return(Ok(connectorUpdated));
        }
예제 #5
0
        public void Should_calculate_binary_Search_second_set()
        {
            using (var context = new ApplicationDbContext(ContextOptions))
            {
                List <Connector> connectors = new List <Connector>();
                connectors.Add(new Connector(1, 10));
                connectors.Add(new Connector(2, 19));
                connectors.Add(new Connector(3, 30));
                connectors.Add(new Connector(4, 40));



                RemoveSuggestions removeSuggestion = new RemoveSuggestions();
                Assert.DoesNotThrow(() => removeSuggestion.GenerateAllSuggestions(connectors, 50f));

                Assert.AreEqual(1, removeSuggestion.Count);
                Assert.AreEqual(2, removeSuggestion[0].Count);
            }
        }
예제 #6
0
        public async Task <ActionResult <ChargeStationResource> > PostChargeStation(Guid groupId, SaveChargeStationResource saveChargeStation)
        {
            Group group = await _context.Group.Include(g => g.ChargeStations).ThenInclude(c => c.Connectors).FirstOrDefaultAsync(g => g.Id == groupId);

            if (group == null)
            {
                return(NotFound("Group not found"));
            }

            ////improve efficiency with a segmented n-ary tree
            if (group.HasExceededCapacity(saveChargeStation.Connectors.Sum(c => c.MaxCurrentAmp).Value))
            {
                List <Connector> connectors        = _context.Connector.Where(c => c.ChargeStation.GroupId == groupId).OrderBy(o => o.MaxCurrentAmp).ToList <Connector>();
                float            excdeededCapacity = group.GetExceededCapacity();

                RemoveSuggestions removeSuggestions = new RemoveSuggestions();

                if (connectors.Count == 0)
                {
                    throw new CapacityExceededException(excdeededCapacity, removeSuggestions);
                }

                removeSuggestions.GenerateAllSuggestions(connectors, excdeededCapacity);
                throw new CapacityExceededException(excdeededCapacity, removeSuggestions);
            }

            ChargeStation chargeStation = _mapper.Map <ChargeStation>(saveChargeStation);

            group.AppendChargeStation(chargeStation);

            await _context.ChargeStation.AddAsync(chargeStation);

            await _context.SaveChangesAsync();

            ChargeStationResource chargeStationResponse = _mapper.Map <ChargeStationResource>(chargeStation);

            return(CreatedAtAction(nameof(GetChargeStation), new { id = chargeStation.Id, groupId = chargeStation.GroupId }, chargeStationResponse));
        }
        public async Task <ActionResult <ConnectorResource> > Post(Guid groupId, Guid chargeStationId, [FromBody] SaveConnectorResource saveConnector)
        {
            Group group = await _context.Group.Include(g => g.ChargeStations).ThenInclude(c => c.Connectors).FirstOrDefaultAsync(g => g.Id == groupId);

            if (group == null)
            {
                return(NotFound("Group not found"));
            }

            Connector     connector = _mapper.Map <Connector>(saveConnector);
            ChargeStation chargeStation;

            if (!group.ChargeStations.TryGetValue(new ChargeStation(chargeStationId), out chargeStation))
            {
                return(NotFound("Charge station not found"));
            }

            if (group.HasExceededCapacity(saveConnector.MaxCurrentAmp.Value))
            {
                List <Connector> connectors        = _context.Connector.Where(c => c.ChargeStation.GroupId == groupId).OrderBy(o => o.MaxCurrentAmp).ToList <Connector>();
                float            excdeededCapacity = group.GetExceededCapacity();

                RemoveSuggestions removeSuggestions = new RemoveSuggestions();
                removeSuggestions.GenerateAllSuggestions(connectors, excdeededCapacity);

                throw new CapacityExceededException(excdeededCapacity, removeSuggestions);
            }
            chargeStation.SyncConnectorIds();
            chargeStation.AppendConnector(connector);

            await _context.SaveChangesAsync();

            ConnectorResource connectorResponse = _mapper.Map <ConnectorResource>(connector);

            return(CreatedAtAction(nameof(GetConnector), new { chargeStationId = chargeStation.Id, groupId = chargeStation.GroupId, id = connector.Id.Value }, connectorResponse));
        }
예제 #8
0
        public async Task <IActionResult> Patch(Guid id, [FromBody] PatchGroupResource value)
        {
            Group group = await _context.Group.Include(g => g.ChargeStations).ThenInclude(c => c.Connectors).FirstOrDefaultAsync(g => g.Id == id);

            if (group == null)
            {
                return(StatusCode(404));
            }

            if (value.Capacity.HasValue)
            {
                if (value.Capacity.Value < group.Capacity && group.HasExceededCapacity(group.Capacity - value.Capacity.Value))
                {
                    List <Connector> connectors        = (List <Connector>)_context.Connector.Where(c => c.ChargeStation.GroupId == id).OrderBy(o => o.MaxCurrentAmp);
                    float            excdeededCapacity = group.GetExceededCapacity();


                    RemoveSuggestions removeSuggestions = new RemoveSuggestions();
                    removeSuggestions.GenerateAllSuggestions(connectors, excdeededCapacity);
                    throw new CapacityExceededException(excdeededCapacity, removeSuggestions);
                }

                group.Capacity = value.Capacity.Value;
            }

            if (!string.IsNullOrEmpty(value.Name))
            {
                group.Name = value.Name;
            }

            await _context.SaveChangesAsync();

            GroupResource updatedGroup = _mapper.Map <GroupResource>(group);

            return(Ok(updatedGroup));
        }
 public CapacityExceededException(float exceededCapacity, RemoveSuggestions removeSuggestions) :
     base($"The group capacity has exceeded by {exceededCapacity}A, remove the suggested connectors to free up space")
 {
     ExceededCapacity  = exceededCapacity;
     RemoveSuggestions = removeSuggestions;
 }