public ResponseModel BackupCustomerOrderData() { ResponseModel res = new ResponseModel(); try { using (CustomerDBEntities db = new CustomerDBEntities()) { List <CustomerOrder> lstCustomerOrders = db.CustomerOrders.Where(x => x.IsAddedToBkpTbl != true).ToList(); foreach (var order in lstCustomerOrders) { BkpCustomerOrder customerOrder = new BkpCustomerOrder(); customerOrder.BkpID = Guid.NewGuid(); customerOrder.order_number = order.order_number; customerOrder.qty = order.qty; customerOrder.user_id = order.user_id; customerOrder.FileUploadedId = order.FileUploadedId; customerOrder.BkpDate = DateTime.Now; db.BkpCustomerOrders.Add(customerOrder); db.SaveChanges(); order.IsAddedToBkpTbl = true; db.SaveChanges(); } } res.status = true; } catch (Exception ex) { res.message = ex.Message; res.data = ex.InnerException; } return(res); }
public IHttpActionResult PutCustomer(int id, Customer customer) { if (id != customer.CustomerID) { return(BadRequest()); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Customer_No,Customer_Name")] Customer customer) { if (ModelState.IsValid) { db.Customers.Add(customer); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customer)); }
public ResponseModel UpsertOrderData() { ResponseModel res = new ResponseModel(); try { //BackUp Customer Order Data to Backup Table in Database res = BackupCustomerOrderData(); if (res.status) { //Upsert Customer Order Data using (CustomerDBEntities db = new CustomerDBEntities()) { List <TempCustomerOrder> lstTmpCustomerOrders = db.TempCustomerOrders.Where(x => x.IsAddedToMainTbl != true).ToList(); foreach (var tmpOrder in lstTmpCustomerOrders) { CustomerOrder customerOrder = db.CustomerOrders.Where(x => x.user_id == tmpOrder.user_id && x.order_number == tmpOrder.order_number).FirstOrDefault(); if (customerOrder != null) { customerOrder.qty = tmpOrder.qty; customerOrder.FileUploadedId = tmpOrder.FileUploadedId; customerOrder.UpdatedDate = DateTime.Now; customerOrder.IsAddedToBkpTbl = false; db.SaveChanges(); } else { customerOrder = new CustomerOrder(); customerOrder.ID = Guid.NewGuid(); customerOrder.order_number = tmpOrder.order_number; customerOrder.qty = tmpOrder.qty; customerOrder.user_id = tmpOrder.user_id; customerOrder.FileUploadedId = tmpOrder.FileUploadedId; customerOrder.CreatedDate = DateTime.Now; customerOrder.IsAddedToBkpTbl = false; db.CustomerOrders.Add(customerOrder); db.SaveChanges(); } tmpOrder.IsAddedToMainTbl = true; db.SaveChanges(); } } res.status = true; } } catch (Exception ex) { res.message = ex.Message; res.data = ex.InnerException; } return(res); }
public HttpResponseMessage Put(int id, [FromBody] Customer customer) { try { using (CustomerDBEntities entity = new CustomerDBEntities()) { var cust = entity.Customers.FirstOrDefault(x => x.ID == id); if (cust != null) { cust.FirstName = customer.FirstName; cust.LastName = customer.LastName; cust.Email = customer.Email; cust.Mobile = customer.Mobile; cust.Address = customer.Address; entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, cust.ToString())); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, id.ToString() + "Not found!")); } } }catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public IHttpActionResult PostUser(User user) { db.Users.Add(user); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = user.UserId }, user)); }
public string SaveProduct(ProductViewModel productViewModel) { Product product = new Product() { Name = productViewModel.Name, Price = productViewModel.Price, Quantity = productViewModel.Quantity, }; if (product != null) { _db.Products.Add(product); _db.SaveChanges(); return(" Save Sucessfully "); } return("Failed "); }
public string SaveUser(UserViewModel userViewModel) { User user = new User() { UserName = userViewModel.UserName, EmailAddress = userViewModel.EmailAddress, Password = userViewModel.Password, ConfirmPassword = userViewModel.ConfirmPassword }; if (user != null) { _db.Users.Add(user); _db.SaveChanges(); return("Sucessfully Registration"); } return("Failed Registration"); }
public HttpResponseMessage Post([FromBody] Customer customer) { try { using (CustomerDBEntities entity = new CustomerDBEntities()) { entity.Customers.Add(customer); entity.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, customer); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (CustomerDBEntities entity = new CustomerDBEntities()) { var deletedCustomer = entity.Customers.Remove(entity.Customers.FirstOrDefault(x => x.ID == id)); if (deletedCustomer == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, "Delete for id" + id.ToString() + "unsuccessfull")); } else { entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, deletedCustomer)); } } }catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public ResponseModel UploadCustomerOrder(System.Web.HttpRequest httpRequest, string rootPath) { ResponseModel res = new ResponseModel(); try { if (httpRequest.Files.Count > 0) { foreach (string file in httpRequest.Files) { HttpPostedFile fileUploaded = httpRequest.Files[file]; string fileName = UtilityHelper.SaveUploadedFile(fileUploaded, rootPath); string filePath = Path.Combine(rootPath, fileName); if (fileName != "") { using (var reader = new StreamReader(filePath)) using (var csv = new CsvReader(reader)) { int rowCnt = 0; var record = new CustomerOrderModel(); var records = csv.EnumerateRecords(record); using (CustomerDBEntities db = new CustomerDBEntities()) { FileStorageInfo fileInfo = new FileStorageInfo(); fileInfo.FileUploadId = Guid.NewGuid(); fileInfo.OriginalFileName = fileUploaded.FileName; fileInfo.StoredFileName = fileName; fileInfo.CreatedDate = DateTime.Now; db.FileStorageInfoes.Add(fileInfo); db.SaveChanges(); foreach (var r in records) { TempCustomerOrder tmpOrder = new TempCustomerOrder(); tmpOrder.ID = Guid.NewGuid(); tmpOrder.order_number = r.order_number; tmpOrder.qty = r.qty; tmpOrder.user_id = r.user_id; tmpOrder.FileUploadedId = fileInfo.FileUploadId; tmpOrder.CreatedDate = DateTime.Now; tmpOrder.IsAddedToMainTbl = false; db.TempCustomerOrders.Add(tmpOrder); db.SaveChanges(); rowCnt++; } } if (rowCnt > 0) { //Upsert Customer Order Data from Staginig table to Main Table res = UpsertOrderData(); if (res.status) { res.status = true; res.message = "File Upload Completed Successfully...!!!"; res.data = GetAllCustomerOrders(); } return(res); } else { res.message = "No/Invalid Data in File...!!!"; return(res); } } } } } return(res); } catch (Exception ex) { if (ex.Source == "CsvHelper") { res.message = "Invalid File Data...!!!"; } else { res.message = ex.Message; } res.data = ex.InnerException; return(res); } }
public static void InsertCustomer(Customer cust) { var myDb = new CustomerDBEntities(); myDb.Customers.Add(cust); myDb.SaveChanges(); }