public IHttpActionResult CreateProductWithProductDetail([FromBody] CreateNReadModel databody)
        {
            using (var db = new DB_context())
            {
                try
                {
                    ProductValidator val = new ProductValidator();
                    //Dictionary<string, ProductViewModel> dict = new Dictionary<string, ProductViewModel>();
                    Dictionary <string, object> result = new Dictionary <string, object>();
                    //Product product = dict.Add(, databody);
                    Product product = databody.CreateProductDictionary();
                    db.Products.Add(product);
                    db.SaveChanges();

                    if (val.isValidProductDetail(product.ProductDetail, product.ProductType) == true)
                    {
                        result.Add("Message", "Insert Data Success");
                    }
                    else
                    {
                        result.Add("Message", "Data is invalid ");
                    }

                    return(Ok(result));
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        public IHttpActionResult calculateProductUnitPrice([FromBody] ProductDetailCalculatorParameter parameter)
        {
            try
            {
                using (var db = new DB_context())
                {
                    var temp = db.Products.AsQueryable();
                    Dictionary <string, object> result = new Dictionary <string, object>();
                    var listProduct = db.Products.OrderByDescending(data => data.ProductID).ToList();

                    ProductCalculator calculator = new ProductCalculator(';');
                    foreach (var item in listProduct)
                    {
                        calculator.calculateProductUnitPrice(item, parameter);
                    }

                    db.SaveChanges();
                    return(Ok("Data Saved Successfully"));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #3
0
파일: Players.cs 프로젝트: Ghilea/CardGame
        public static void SavePlayerNameAndHighscore()
        {
            using (var context = new DB_context())
            {
                DB_player player = new DB_player();
                player.Name = PlayerName;
                context.DPlayer.Add(player);
                context.SaveChanges();

                DB_highScore highscore = new DB_highScore();
                highscore.Points = PlayerPoints;
                highscore.Player = context.DPlayer.Where(h => h.Id == player.Id).First();

                context.DHighScore.Add(highscore);
                context.SaveChanges();
            }
        }
 public void Post([FromBody] User user)
 {
     // Hvis user var DTO...
     // ny indstans af bil f.eks. Car c = new Car();
     // c.registernr = TotalDTO.reg
     // man laver flere _context.XXX også savechanges til sidst 1 gang. Ellers må man gerne lave savechanges flere gange, hvor man bruger await til at håndtere hvad der bliver gemt først.. Eksempel hvis bilen har user id som foreign key, så skal user regisreres før bilen.
     using (_context)
     {
         _context.User.Add(user);
         _context.SaveChanges();
     }
 }
예제 #5
0
        private bool SaveReadout(double new_readout_value)
        {
            // readout == 0 --> create new entry
            if (readout_ID == 0)
            {
                try
                {
                    using (DB_context context = new DB_context())
                    {
                        tbl_Readouts readout = new tbl_Readouts();

                        readout.ID_METER        = Convert.ToInt32(cmbMeters.SelectedValue);
                        readout.READOUT_DATE    = dp_readoutAdded.SelectedDate.Value;
                        readout.READOUT_VALUE   = new_readout_value;
                        readout.READOUT_COMMENT = txtComment.Text.Trim();

                        // add new entry
                        context.db_Readouts.Add(readout);
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving to DB. \r\n\r\n" + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }
            // readout != 0 --> edit existing entry
            else
            {
                try
                {
                    using (DB_context context = new DB_context())
                    {
                        var query = (from c in context.db_Readouts where c.ID_READOUT == readout_ID select c).FirstOrDefault();
                        query.READOUT_DATE    = dp_readoutAdded.SelectedDate.Value;
                        query.READOUT_VALUE   = new_readout_value;
                        query.READOUT_COMMENT = txtComment.Text.Trim();

                        // save edited entry
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving to DB. \r\n\r\n" + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }
            return(true);
        } // save readout
        public Task <Order> StartAsync(string orderId, OrderStatuses status)
        {
            //var order = GetById(orderId);
            ////order.Start();
            //var orderEvent = new OrderEvent(order.Id, order.Name, OrderStatuses.PROCESSING, DateTime.Now);
            //_events.AddEvent(orderEvent);
            //return Task.FromResult(order);
            Order order = null;

            using (var context = new DB_context())
            {
                order = context.Orders.SingleOrDefault(p => p.Id == orderId);

                order.Status = status;
                context.SaveChanges();
            }
            return(Task.FromResult(order));
        }
예제 #7
0
        } // edit meter

        private void DeleteReadout(object sender, RoutedEventArgs e)
        {
            int ID_meter = 0;

            /*
             * find selected row, get ID from first column (hidden column "ID")
             * find entry in DB based on ID,
             * delete entry, save changes
             */
            MessageBoxResult result = MessageBox.Show("Delete this entry?", "Info", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
                {
                    if (vis is DataGridRow)
                    {
                        var row = (DataGridRow)vis;
                        int id  = Convert.ToInt32((tbl_Readouts.SelectedCells[0].Column.GetCellContent(row) as TextBlock).Text);

                        using (DB_context context = new DB_context())
                        {
                            var query = (from c in context.db_Readouts where c.ID_READOUT == id select c).FirstOrDefault();

                            ID_meter = query.ID_METER;

                            context.db_Readouts.Remove(query);
                            context.SaveChanges();

                            MessageBox.Show("Success!", "Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                        } // using
                    }     // if is datagrid row
                }         // for ... cycle
            }             // if result == yes
            else
            {
                return;
            }

            // update list on finish
            LoadReadoutsForMeter(ID_meter);
        }
        public Task <Order> CreateAsync(Order order)
        {
            using (var context = new DB_context())
            {
                context.Orders.Add(order);
                context.SaveChanges();
            }


            //event/Subscripcions
            var orderEvent = new OrderEvent()
            {
                Id        = order.Id,
                OrderId   = order.Id,
                Name      = order.Name,
                Statuses  = order.Status,
                Timestamp = order.Created
            };

            _events.AddEvent(orderEvent);
            return(Task.FromResult(order));
        }
        } // edit meter

        // function for deleting Meter and corresponding readings
        private void DeleteMeter(object sender, RoutedEventArgs e)
        {
            /*
             * find selected row, get ID from first column (hidden column "ID")
             * find entry in DB based on ID,
             */
            MessageBoxResult result = MessageBox.Show("Delete this meter and this meter readouts?", "Info", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
                {
                    if (vis is DataGridRow)
                    {
                        var row = (DataGridRow)vis;
                        int id  = Convert.ToInt32((tblMeters.SelectedCells[0].Column.GetCellContent(row) as TextBlock).Text);

                        using (EnergyMetersEntities context = new EnergyMetersEntities())
                        {
                            /*
                             * TRY Using stored procedure to delete meter from tbl_Meters
                             * and all readouts with matching ID_METER from tbl_Readouts
                             *
                             * IN CASE OF STORED PROCEDURE FAIL
                             * try using linq expression
                             */
                            try
                            {
                                context.DeleteMeterAndReadouts(id);
                                MessageBox.Show("Success!", "Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                            }
                            catch
                            {
                                try
                                {
                                    using (DB_context EF_context = new DB_context())
                                    {
                                        var meter = (from c in EF_context.db_Meters where c.ID_METER == id select c).FirstOrDefault();

                                        EF_context.db_Meters.Remove(meter);

                                        var readouts = (from c in EF_context.db_Readouts where c.ID_METER == id select c).ToList();

                                        foreach (var item in readouts)
                                        {
                                            EF_context.db_Readouts.Remove(item);
                                        }

                                        EF_context.SaveChanges();

                                        MessageBox.Show("Success!", "Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                                    } // using
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Error, DataBase is not available.\r\n\r\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                                }
                            } // catch Stored procedure
                        }     // using ...
                    }         // if is datagrid row
                }             // for ... cycle
            }                 // if result == yes

            // update list on finish
            LoadMeterList();
        } // delete meter
예제 #10
0
        private bool SaveToDB()
        {
            Regex regex     = new Regex(@"(^[A-Za-z0-9-.\s]*$)"); // regex for letters, numbers, '-' and spaces
            Match matchName = regex.Match(txtMeterName.Text.Trim());

            if (!matchName.Success)
            {
                MessageBox.Show("Inappropriate Meter name, please choose another." + "\r\n" + "Allowed only leters, digits and spaces", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            if (cmbMeterType.Text.Contains("-"))
            {
                MessageBox.Show("Select Meter type", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            if (cmbMeterUnits.Text.Contains("-"))
            {
                MessageBox.Show("Select Meter units", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            /*
             * if no ID was sent --> create new entry in DB
             * else get entry by ID, update entry and save changes
             */

            if (meter_ID == 0) // new entry
            {
                // try save to DB
                try
                {
                    using (DB_context context = new DB_context())
                    {
                        tbl_Meters new_meter = new tbl_Meters();

                        new_meter.METER_NAME  = txtMeterName.Text.Trim();
                        new_meter.METER_TYPE  = cmbMeterType.Text;
                        new_meter.METER_UNITS = cmbMeterUnits.Text;
                        // add new
                        context.db_Meters.Add(new_meter);
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving to DB. \r\n\r\n" + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }
            else // edit entry
            {
                try
                {
                    using (DB_context context = new DB_context())
                    {
                        tbl_Meters new_meter = new tbl_Meters();

                        var query = (from c in context.db_Meters where c.ID_METER == meter_ID select c).FirstOrDefault();

                        query.METER_NAME  = txtMeterName.Text.Trim();
                        query.METER_TYPE  = cmbMeterType.Text;
                        query.METER_UNITS = cmbMeterUnits.Text;

                        // save edited entry
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving to DB. \r\n\r\n" + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }
            return(true);
        } // save to DB