public CoreProduct Get(int id) { Products ObjProducts = new Products(); CoreProduct ObjCoreProduct = ObjProducts.GetProduct(id); return(ObjCoreProduct); }
/// <summary> /// This private method get and return a CoreProduct Object by Id /// </summary> /// <param name="id">This is the primary key of record on Table</param> /// <returns>CoreProduct Object</returns> private CoreProduct GetProductInner(int id) { CoreProduct ObjCoreProduct = this._context.CoreProducts .Where(r => r.Id == id) .FirstOrDefault(); return(ObjCoreProduct); }
public void InsertProduct() { using (var connection = new SqlConnection(conn_str)) { try { //set the number of elements present inside Amazon0302.txt file int number = 262111; //foreach of those entries create a product and insert inside the dataset List <CoreProduct> products = new List <CoreProduct>(); Random rand = new Random(); Random price = new Random(); for (int i = 0; i < number; i++) { int tmp_date = rand.Next(0, 730); //two year span decimal tmp_price = price.Next(10, 1000); CoreProduct tmp = new CoreProduct() { ProdID = i, Price = tmp_price, InsertionDate = DateTime.Now.AddDays(-tmp_date) }; products.Add(tmp); if (i % 10000 == 0) { //db.tblCoreProduct.AddRange(products); //db.SaveChanges(); connection.BulkInsert(products); //empty the list products.Clear(); //db = new CoreProductContext(); } } //check remaining if (products.Count() > 0) { //db = new CoreProductContext(); //db.tblCoreProduct.AddRange(products); //db.SaveChanges(); connection.BulkInsert(products); products.Clear(); } }catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
public ResponseApi Post(int companyId, [FromBody] CoreProduct FormData) { bool resultTransaction = new Products().AddProduct(companyId, FormData.Name, (int)FormData.Cost, FormData.Reference, (int)FormData.Quantity); ResponseApi ObjResponseApi = new ResponseApi(); ObjResponseApi.Result = resultTransaction; if (resultTransaction) { ObjResponseApi.Message = "The product was created!"; } else { ObjResponseApi.Message = "Error, try again."; } return(ObjResponseApi); }
/// <summary> /// This private method delete record from parameters method /// </summary> /// <param name="id">Primary key table</param> /// <returns>Bool value as result of transaction</returns> private bool DeleteProductInner(int id) { try { CoreProduct ObjCoreProduct = this._context.CoreProducts .Where(r => r.Id == id) .FirstOrDefault(); this._context.CoreProducts.Remove(ObjCoreProduct); this._context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// This private method updated a record in table /// </summary> /// <returns>Bool value as result of transaction</returns> private bool UpdateProductInner() { try { CoreProduct ObjCoreProduct = this._context.CoreProducts .Where(r => r.Id == this._id) .FirstOrDefault(); ObjCoreProduct.Name = this._name; ObjCoreProduct.Cost = this._cost; ObjCoreProduct.Reference = this._reference; ObjCoreProduct.Quantity = this._quantity; this._context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// This public method add new record in database from parameters method /// </summary> /// <param name="identification">Identification of company, as NIT in Colombia</param> /// <param name="name">Name of company</param> /// <returns>Bool value as result of transaction</returns> private bool AddProductInner(int companyId, string name, int cost, string reference, int quantity) { try { CoreProduct ObjCoreProduct = new CoreProduct(); ObjCoreProduct.CompanyId = companyId; ObjCoreProduct.Name = name; ObjCoreProduct.Cost = cost; ObjCoreProduct.Reference = reference; ObjCoreProduct.Quantity = quantity; ObjCoreProduct.CreatedAt = DateTime.Now; this.AddInner(ObjCoreProduct); return(true); } catch (Exception e) { return(false); } }
/// <summary> /// This private method get private properties and It add new record on table /// </summary> /// <returns>Bool value as result of transaction</returns> private bool AddProductInner() { try { CoreProduct ObjCoreProduct = new CoreProduct(); ObjCoreProduct.CompanyId = this._companyId; ObjCoreProduct.Name = this._name; ObjCoreProduct.Cost = this._cost; ObjCoreProduct.Reference = this._reference; ObjCoreProduct.Quantity = this._quantity; ObjCoreProduct.CreatedAt = DateTime.Now; this.AddInner(ObjCoreProduct); return(true); } catch (Exception) { return(false); } }
/// <summary> /// This private method update a record in database from parameters method /// </summary> /// <param name="id">Primary key table</param> /// <param name="identification">Identification of company, as NIT in Colombia</param> /// <param name="name">Name of company</param> /// <returns>Bool value as result of transaction</returns> private bool UpdateProductInner(int id, string name, int cost, string reference, int quantity) { try { CoreProduct ObjCoreProduct = this._context.CoreProducts .Where(r => r.Id == id) .FirstOrDefault(); ObjCoreProduct.Name = name; ObjCoreProduct.Cost = cost; ObjCoreProduct.Reference = reference; ObjCoreProduct.Quantity = quantity; this._context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// The function creates purchase for each client inside the product list, for optimization reasons /// a bins of 10000 users and 10000 elements is choosen each time to avoid problems with the /// number of records inside the tables /// </summary> public void InsertPurchase() { using (var connection = new SqlConnection(conn_str)) { try { //int prodNum = 262110; //int usrNum = 20000; int prodNum = connection.ExecuteScalar <int>("SELECT COUNT(*) FROM CoreProduct"); int usrNum = connection.ExecuteScalar <int>("SELECT COUNT(*) FROM CoreUser"); //create random purchase for each user Random usrRand = new Random(); Random prodRand = new Random(); Random dayRand = new Random(); Random qtaRand = new Random(); //set a number of purchase -> for base purchasing int purchaseNumber = 300000; List <CorePurchase> purchase = new List <CorePurchase>(); for (int i = 0; i < purchaseNumber; i++) { int ui = usrRand.Next(0, usrNum); int pi = prodRand.Next(1, prodNum); int qta = qtaRand.Next(0, 100); int day = dayRand.Next(0, 730); //CoreUser tmp_user = db_user.tblCoreUser.Find(ui); //CoreProduct tmp_prod = db_prod.tblCoreProduct.Find(pi); CoreUser tmp_user = connection.QueryFirst <CoreUser>("SELECT * FROM CoreUser WHERE ID=@UserID", new { UserID = ui }); CoreProduct tmp_prod = connection.QueryFirst <CoreProduct>("SELECT * FROM CoreProduct WHERE ProdID=@ProdID", new { ProdID = pi }); //create the association CorePurchase cp = new CorePurchase() { ProductID = tmp_prod.ProdID, UserID = tmp_user.ID, Quantity = qta, PurchaseDate = DateTime.Now.AddDays(-day) }; purchase.Add(cp); //wait for 1000 and insert if (i % 10000 == 0) { //db.AddRange(purchase); //db.SaveChanges(); connection.BulkInsert(purchase); purchase.Clear(); //db = new CorePurchaseContext(); } } if (purchase.Count > 0) { //db = new CorePurchaseContext(); //db.AddRange(purchase); //db.SaveChanges(); connection.BulkInsert(purchase); purchase.Clear(); } }catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// This private method execute add trigger on db context /// </summary> /// <param name="ObjCoreProduct">Object to add</param> private void AddInner(CoreProduct ObjCoreProduct) { this._context.CoreProducts.Add(ObjCoreProduct); this._context.SaveChanges(); }