Пример #1
0
        public async Task <IHttpActionResult> PostProsumer(ProsumerDTO[] prosumer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var uow = new UnitOfWork <Prosumer>(_db);

            var allProsumers = uow.Repository.ReadAll();

            foreach (var pro in prosumer)
            {
                var oldProsumer =
                    (from op in allProsumers
                     where op.Name.Equals(pro.Name, StringComparison.CurrentCultureIgnoreCase)
                     select op).FirstOrDefault();

                if (oldProsumer == null)
                {
                    uow.Repository.Create(new Prosumer()
                    {
                        Consumed          = pro.Consumed,
                        Produced          = pro.Produced,
                        Name              = pro.Name,
                        PreferedBuyerName = pro.PreferedBuyer,
                        Type              = pro.Type,
                        Difference        = pro.Produced - pro.Consumed,
                        Remainder         = pro.Produced - pro.Consumed
                    });
                    continue;
                }

                oldProsumer.Consumed          = pro.Consumed;
                oldProsumer.Produced          = pro.Produced;
                oldProsumer.Name              = pro.Name;
                oldProsumer.PreferedBuyerName =
                    pro.PreferedBuyer; //allProsumers.Find(pro.PreferedBuyer) != null ? pro.PreferedBuyer : string.Empty;
                oldProsumer.Type       = pro.Type;
                oldProsumer.Difference = pro.Produced - pro.Consumed;
                oldProsumer.Remainder  = pro.Produced - pro.Consumed;

                uow.Repository.Update(string.Empty, oldProsumer);
            }

            try
            {
                uow.Commit();
            }
            catch (DbUpdateException)
            {
                return(BadRequest("You don goofed"));
            }

            await PowerDistributer.HandleTransactions();

            //return CreatedAtRoute("DefaultApi", new { id = prosumer.Name }, prosumer);
            return(Ok());
        }
Пример #2
0
        // GET: api/Prosumers
        public async Task <IEnumerable <ProsumerDTO> > GetProsumers()
        {
            var uow = new UnitOfWork <Prosumer>(db);

            var Dtos = new List <ProsumerDTO>();

            foreach (var pro in uow.Repository.ReadAll())
            {
                Dtos.Add(new ProsumerDTO(pro));
            }

            await PowerDistributer.HandleTransactions();

            return(Dtos);
        }