public HttpResponseMessage Delete(int id) { try { using (databaseEntities dbEntities = new databaseEntities()) { var entity = dbEntities.Lends.FirstOrDefault(loanId => loanId.Id == id); if (entity != null) { //remove from database dbEntities.Lends.Remove(entity); dbEntities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "There is no loan associated with the requested id: " + id.ToString())); } } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public bool UserExists(string username) { using (var context = new databaseEntities()) { return((from x in context.Users where x.Username == username select x).Count() == 1); } }
public IEnumerable <Lend> GetAllLoans() { using (databaseEntities dbEntities = new databaseEntities()) { return(dbEntities.Lends.ToList()); } }
public Transaction CreateNewTransaction(int userId, List <int> productIds, List <int> productAmounts) { using (var context = new databaseEntities()) { var u = (from x in context.Users where x.Id == userId select x).First(); var purchases = new List <Purchase>(); for (int i = 0; i < productIds.Count; i++) { var currentId = productIds[i]; var currentAmount = productAmounts[i]; purchases.Add(new Purchase(currentId, currentAmount, context.Products.Where(x => x.Id == currentId).First().Price)); } var totalPrice = purchases.Sum(x => x.TotalPrice); var stocks = (from x in context.Stocks from y in productIds where x.ProductId == y select x); if (!(u.Funds >= totalPrice)) { return(null); } for (int i = 0; i < productIds.Count; i++) { var currentId = productIds[i]; var currentAmount = productAmounts[i]; var stock = stocks.Single(x => x.Id == currentId); if (stock.Amount < currentAmount) { return(null); } } var t = new Transaction { UserId = userId }; context.Transactions.Add(t); u.Funds -= totalPrice; for (int i = 0; i < productIds.Count; i++) { var currentId = productIds[i]; var currentAmount = productAmounts[i]; var currentPrice = (from x in context.Products where x.Id == currentId select x).First().Price; (from y in stocks where y.ProductId == currentId select y).First().Amount -= currentAmount; context.TransactionRows.Add(new TransactionRow { ProductId = currentId, Amount = currentAmount, TransactionId = t.Id, Price = currentPrice }); } context.SaveChanges(); return(t); } }
public List <TransactionModel> GetAllTransactions() { using (var context = new databaseEntities()) { var result = new List <TransactionModel>(); (from x in context.Transactions select x).ToList().ForEach(x => result.Add(new TransactionModel(x))); return(result); } }
public List <UserModel> GetAllUsers() { using (var context = new databaseEntities()) { var result = new List <UserModel>(); (from x in context.Users select x).ToList().ForEach(x => result.Add(new UserModel(x))); return(result); } }
public List <ProductModel> GetAllAvailibleProducts() { using (var context = new databaseEntities()) { var result = new List <ProductModel>(); (from x in context.Products from y in context.Stocks where y.Amount > 0 && y.ProductId == x.Id select y).ToList().ForEach(x => result.Add(new ProductModel(x.Product, x))); return(result); } }
public User CreateNewUser(string username) { using (var context = new databaseEntities()) { var u = new User { Username = username, Password = new string(username.ToCharArray().Reverse().ToArray()), Funds = 100 }; context.Users.Add(u); context.SaveChanges(); return(u); } }
public TransactionModel GetTransactionById(int id) { try { using (var context = new databaseEntities()) { return(new TransactionModel((from x in context.Transactions where x.Id == id select x).First())); } } catch { return(null); } }
public UserModel LoginUser(string username, string password) { try { using (var context = new databaseEntities()) { return(new UserModel((from x in context.Users where x.Username == username && x.Password == password select x).First())); } } catch { return(null); } }
public UserModel GetUserById(int id) { try { using (var context = new databaseEntities()) { return(new UserModel((from x in context.Users where x.Id == id select x).First())); } } catch { return(null); } }
public ProductModel GetProductById(int id) { try { using (var context = new databaseEntities()) { Stock s = (from x in context.Stocks where x.ProductId == id select x).First(); Product p = s.Product; return(new ProductModel(p, s)); } } catch { return(null); } }
//[Route("{id}")] public Lend GetLoan(int id) { using (databaseEntities dbEntities = new databaseEntities()) { return(dbEntities.Lends.FirstOrDefault(loanId => loanId.Id == id)); /*if(dbEntities != null) * { * return Request.CreateResponse(HttpStatusCode.OK, dbEntity); * } * else * { * return Request.CreateErrorResponse(HttpStatusCode.NotFound, "There is no loan associated with the requested id: " + id.ToString()); * } */ } }
//[Route("post")] public HttpResponseMessage Post(Lend loan) { try { using (databaseEntities dbEntities = new databaseEntities()) { //add to database dbEntities.Lends.Add(loan); dbEntities.SaveChanges(); //get the response code of 201 for successful post and return the newly added loan var message = Request.CreateResponse(HttpStatusCode.Created, loan); message.Headers.Location = new Uri(Request.RequestUri + loan.Id.ToString()); return(message); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
private void fillDataGrid(object sender, RoutedEventArgs e) { context = new databaseEntities(); matches = new ObservableCollection <table_match>(); foreach (table_match match in context.table_match) { matches.Add( new table_match { home_team = match.home_team + " - " + match.away_tema, star__of_the_match = match.star__of_the_match, home_team_odds = match.home_team_odds, even_odds = match.even_odds, away_team_odds = match.away_team_odds, home_team_or_even_odds = match.home_team_or_even_odds, away_team_or_even_odds = match.away_team_or_even_odds, home_or_away_team_odds = match.home_or_away_team_odds, handicap_odds = match.handicap_odds, category_id = match.category_id }); } DataGrid.ItemsSource = matches; }