Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.ServiceModel;
- using System.Runtime.Serialization;
- namespace StoreDomain
- {
- public class Store
- {
- public Store() { }
- public Users GetUser(String username)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- var users = from u in ctx.UsersSet
- where u.Name == username
- select u;
- foreach (Users user in users)
- {
- return user;
- }
- return null;
- }
- }
- public UsersDTO GetUsersDTO(String username)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- var users = from u in ctx.UsersSet
- where u.Name == username
- select u;
- UsersDTO user = new UsersDTO();
- foreach (Users us in users)
- {
- user.Id = us.Id;
- user.Name = us.Name;
- user.Password = us.Password;
- user.Saldo = us.Saldo;
- return user;
- }
- return null;
- }
- }
- public Product GetProduct(int id)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- var products = from p in ctx.ProductSet
- where p.Id == id
- select p;
- foreach (Product product in products)
- {
- return product;
- }
- return null;
- }
- }
- public ProductDTO GetProductDTO(int id)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- var products = from p in ctx.ProductSet
- where p.Id == id
- select p;
- ProductDTO product = new ProductDTO();
- foreach (Product pr in products)
- {
- product.Id = pr.Id;
- product.Name = pr.Name;
- product.Price = pr.Price;
- return product;
- }
- return null;
- }
- }
- public List<ProductDTO> GetAllProducts()
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- List<ProductDTO> allProducts = new List<ProductDTO>();
- var products = from p in ctx.ProductSet
- select p;
- foreach (Product up in products)
- {
- ProductDTO product = new ProductDTO();
- product.Id = up.Id;
- product.Name = up.Name;
- product.Price = up.Price;
- allProducts.Add(product);
- }
- return allProducts;
- }
- }
- public List<ProductDTO> GetAllProductsDTOByUser(int id)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- List<ProductDTO> allProducts = new List<ProductDTO>();
- var products = from p in ctx.UserProductsSet
- where p.UserId == id
- select p;
- foreach (UserProducts p in products)
- {
- var userProducts = from up in ctx.ProductSet
- where up.Id == p.Id
- select up;
- foreach (Product up in userProducts)
- {
- ProductDTO product = new ProductDTO();
- product.Id = up.Id;
- product.Name = up.Name;
- product.Price = up.Price;
- allProducts.Add(product);
- }
- };
- return allProducts;
- }
- }
- public List<Product> GetAllProductsByUser(int id)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- List<Product> allProducts = new List<Product>();
- var products = from p in ctx.UserProductsSet
- where p.UserId == id
- select p;
- foreach (UserProducts p in products)
- {
- var userProducts = from up in ctx.ProductSet
- where up.Id == p.Id
- select up;
- foreach (Product up in userProducts)
- {
- allProducts.Add(up);
- }
- };
- return allProducts;
- }
- }
- public Boolean CanSell(ProductDTO p)
- {
- if (p != null && GetAllProducts().Contains(p))
- {
- return true;
- }
- return false;
- }
- public Boolean CanUserBuy(Users user, ProductDTO p)
- {
- if (p.Price <= user.Saldo)
- {
- return true;
- }
- return false;
- }
- public Boolean SellProduct(String username, ProductDTO p)
- {
- if (CanSell(p) && CanUserBuy(GetUser(username), p))
- {
- AddProductToUser(username, p.Id);
- return true;
- }
- return false;
- }
- private void AddProductToUser(String username, int productId)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- Users user = GetUser(username);
- ProductDTO product = GetProductDTO(productId);
- InventoryItem inventoryItem = ctx.InventoryItemSet.Single(i => i.Id == product.Id);
- inventoryItem.Amount -= 1;
- user.Saldo -= product.Price;
- List<Product> userProducts = GetAllProductsByUser(user.Id);
- if (userProducts.Contains(GetProduct(productId)))
- {
- UserProducts uProduct = ctx.UserProductsSet.Single(i => i.Id == product.Id);
- uProduct.Amount += 1;
- }
- else
- {
- UserProducts up = new UserProducts { ProductId = product.Id, UserId = user.Id, Amount = 1 };
- ctx.UserProductsSet.Add(up);
- }
- ctx.SaveChanges();
- }
- }
- public List<Users> GetAllUsers()
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- List<Users> allUsers = new List<Users>();
- var users = from u in ctx.UsersSet
- select u;
- foreach (Users u in users)
- {
- System.Console.WriteLine(u);
- Debug.WriteLine(u);
- allUsers.Add(u);
- }
- return allUsers;
- }
- }
- public Boolean Register(String username)
- {
- if (!CheckIfUserExists(username))
- {
- char[] charArray = username.ToCharArray();
- Array.Reverse(charArray);
- String password = new string(charArray);
- AddUserToDatabase(username, password);
- return true;
- }
- return false;
- }
- public Boolean CheckIfUserExists(String username)
- {
- if(username == String.Empty && GetAllUsers().Count > 0)
- {
- foreach (Users u in GetAllUsers())
- {
- if (u.Name.Equals(username))
- {
- return true;
- }
- }
- }
- return false;
- }
- public void AddUserToDatabase(String username, String password)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- Users user = new Users { Name = username, Password = password, Saldo = 20.0 };
- ctx.UsersSet.Add(user);
- ctx.SaveChanges();
- }
- }
- public Boolean Login(String username, String password)
- {
- using (mymodelContainer ctx = new mymodelContainer())
- {
- foreach(Users u in GetAllUsers())
- {
- if(u.Name.Equals(username) && u.Password.Equals(password))
- {
- return true;
- }
- }
- return false;
- }
- }
- }
- public partial class Users
- {
- public override Boolean Equals(Object other)
- {
- Boolean b = other.GetType() == typeof(Users);
- if (b)
- {
- Users u = (Users)other;
- b = b && this.Id == u.Id;
- b = b && this.Name.Equals(u.Name);
- b = b && this.Password.Equals(u.Password);
- b = b && this.Saldo == u.Saldo;
- }
- return b;
- }
- public override int GetHashCode()
- {
- return Id.GetHashCode() + Name.GetHashCode() + Password.GetHashCode() + Saldo.GetHashCode();
- }
- }
- public partial class ProductDTO
- {
- public override Boolean Equals(Object other)
- {
- Boolean b = other.GetType() == typeof(ProductDTO);
- if (b)
- {
- ProductDTO p = (ProductDTO)other;
- b = b && this.Id == p.Id;
- b = b && this.Name.Equals(p.Name);
- b = b && this.Price == p.Price;
- }
- return b;
- }
- public override int GetHashCode()
- {
- return Id.GetHashCode() + Name.GetHashCode() + Price.GetHashCode();
- }
- }
- [DataContract]
- public partial class ProductDTO
- {
- [DataMember]
- public int Id { get; set; }
- [DataMember]
- public String Name { get; set; }
- [DataMember]
- public double Price { get; set; }
- }
- [DataContract]
- public partial class UsersDTO
- {
- [DataMember]
- public int Id { get; set; }
- [DataMember]
- public String Name { get; set; }
- [DataMember]
- public String Password { get; set; }
- [DataMember]
- public double Saldo { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement