Esempio n. 1
0
        public override List <Fuel> ToModel(FuelDto dto)
        {
            List <Fuel> fuels = new List <Fuel>();

            if (dto.Fuels == null || !dto.Fuels.Any())
            {
                throw new System.Exception("No params '{fuels}' in the payload , and it should an array of {key : string ,value : number}");
            }

            var co2Price = 0f;

            try
            {
                co2Price = dto.Fuels.First <KeyValuePair <string, float> >(kv => kv.Key.ToLower().StartsWith(FuelParams.CO2)).Value;
            }
            catch
            {
                throw new System.Exception($"mandatory params '{FuelParams.CO2}' missing in the fuels");
            }


            foreach (var f in dto.Fuels.Where(f => !f.Key.StartsWith(FuelParams.CO2)))
            {
                if (f.Key.ToLower().StartsWith(FuelType.KEROSINE.ToString().ToLower()))
                {
                    fuels.Add(new KerosineFuel {
                        PricePerMwh = f.Value, Co2PricePerTon = co2Price
                    });
                }

                else if (f.Key.ToLower().StartsWith(FuelType.GAS.ToString().ToLower()))
                {
                    fuels.Add(new GasFuel {
                        PricePerMwh = f.Value, Co2PricePerTon = co2Price, Co2EmmittedPerMwh = FuelParams.CO2_GAS_PER_MWH
                    });
                }

                else if (f.Key.ToLower().StartsWith(FuelType.WIND.ToString().ToLower()))
                {
                    fuels.Add(new WindFuel {
                        PricePerMwh = 0, Co2PricePerTon = co2Price, WindPower = f.Value
                    });
                }

                else
                {
                    throw new System.Exception($"Unknown Fuel Type '{f.Key}' , " +
                                               $"possible value are '{string.Join("|", Enum.GetNames(typeof(FuelType))).ToLower()}' ");
                }
            }

            return(fuels);
        }
        public void Execute(FuelDto request)
        {
            var fuel = new Fuel();

            if (Context.Fuels.Any(f => f.Type.ToLower() == request.Type.ToLower()))
            {
                throw new EntityAlreadyExistsException("Fuel");
            }
            fuel.Type = request.Type;
            Context.Fuels.Add(fuel);
            Context.SaveChanges();
        }
        public void Execute(FuelDto request)
        {
            var fuel = Context.Fuels.Find(request.Id);

            if (fuel == null)
            {
                throw new EntityNotFoundException("Fuel");
            }
            if (Context.Fuels.Any(f => f.Type.ToLower() == request.Type.ToLower()))
            {
                throw new EntityAlreadyExistsException("Fuel");
            }
            fuel.Type = request.Type;
            Context.SaveChanges();
        }
 public ActionResult Post([FromBody] FuelDto dto)
 {
     try
     {
         _add.Execute(dto);
         return(StatusCode(201));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(StatusCode(500));
     }
 }
        public void CanConvertFuelDtoToModel()
        {
            //setup
            FuelConverter fuelConverter = new FuelConverter();

            FuelDto dto = new FuelDto {
                Fuels = new Dictionary <string, float> {
                    { "gas(euro/MWh)", 13.4f },
                    { "kerosine(euro/MWh)", 50.8f },
                    { "co2(euro/ton)", 20f },
                    { "wind(%)", 60f }
                }
            };

            var models = fuelConverter.ToModel(dto);

            var   gas         = models.Single(f => f.GetType() == typeof(GasFuel));
            float gasPrice    = gas.PricePerMwh;
            float co2PriceGaz = gas.Co2PricePerTon;

            Assert.AreEqual(gasPrice, 13.4f);
            Assert.AreEqual(co2PriceGaz, 20f);



            Fuel  kerosine      = models.Single(f => f.GetType() == typeof(KerosineFuel));
            float kerosinePrice = kerosine.PricePerMwh;
            float co2Kerosine   = kerosine.Co2PricePerTon;

            Assert.AreEqual(kerosinePrice, 50.8f);
            Assert.AreEqual(co2Kerosine, 20f);

            Fuel  wind           = models.Single(f => f.GetType() == typeof(WindFuel));
            float windPrice      = wind.PricePerMwh;
            float co2Wind        = wind.Co2PricePerTon;
            float windPerCentage = ((WindFuel)wind).WindPower;

            Assert.AreEqual(windPrice, 0f);
            Assert.AreEqual(co2Wind, 20f);
            Assert.AreEqual(windPerCentage, 60f);
        }
 public ActionResult Put([FromBody] FuelDto dto)
 {
     try
     {
         _edit.Execute(dto);
         return(StatusCode(204));
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(Conflict(e.Message));
     }
 }
Esempio n. 7
0
 public Task <FuelDto> AddFuelAsync(FuelDto fuelDto)
 {
     throw new NotImplementedException();
 }