Exemplo n.º 1
0
        public static string Get(int profileId)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                var userData = (from b in db.Users
                                where b.Id == profileId
                                select b).FirstOrDefault();

                // if UserData is 0, there was no corresponding userId
                if (userData.Id == 0)
                {
                    throw new Exception("No user exists with that Id.");
                }

                r.Status = 1;
                r.Schema = "GetProfile";
                r.Data.Add("UserName", userData.UserName);
                r.Data.Add("FirstName", userData.FirstName);
                r.Data.Add("LastName", userData.LastName);
                r.Data.Add("PhoneNumber", userData.PhoneNumber);
                r.Data.Add("Rating", userData.Rating.ToString());
                r.Data.Add("EmailAddress", userData.EmailAddress.ToString());
            }

            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 2
0
        public static string GetUserTransactions(int uId)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                var transactions = from t in db.Transactions
                                   where t.UserId == uId &&
                                   t.Active == 1
                                   select t;
                List <ListingDetails> listingList = new List <ListingDetails>();
                foreach (var item in transactions)
                {
                    ListingDetails l = new ListingDetails();
                    l.Author        = item.Product.Author;
                    l.Description   = item.Description;
                    l.ISBN          = item.Product.ISBN;
                    l.ListPrice     = item.Price;
                    l.Name          = item.Product.Title;
                    l.Negotiable    = item.Negotiable;
                    l.Publisher     = item.Product.Publisher;
                    l.UserId        = item.UserId;
                    l.TransactionId = item.Id;

                    listingList.Add(l);
                }
                r.Data.Add("Transactions", listingList);
            }
            r.Status = 1;
            r.Schema = "GetUserTransactions";
            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 3
0
        public static string Get(int transactionId)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                // Get all bids associated with this transaction
                var bids = from b in db.Bids
                           where b.TransactionId == transactionId
                           select b;

                // Return this list of bids
                var bList = new List <BidFlash>();
                foreach (var item in bids)
                {
                    bList.Add(new BidFlash {
                        Id = item.Id, ProposedPrice = item.ProposedPrice
                    });
                }
                //var bidJson = JsonConvert.SerializeObject(bList);

                r.Status = 1;
                r.Schema = "GetBids";
                r.Data.Add("BidList", bList);
            }

            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new listing based on user input
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        public static string Listing(ListingDetails details)
        {
            string response = "";

            using (var db = new TBDataModel())
            {
                int pId = (from b in db.Products
                           where b.ISBN == details.ISBN &&
                           b.Title == details.Name
                           select b.Id).FirstOrDefault();

                // If this product doesnt exist
                if (pId == 0)
                {
                    var newProd = new Product();
                    newProd.Author    = details.Author;
                    newProd.ISBN      = details.ISBN;
                    newProd.Publisher = details.Publisher;
                    newProd.Title     = details.Name;
                    newProd.UserId    = details.UserId;

                    db.Products.Add(newProd);

                    db.SaveChanges();

                    pId = newProd.Id;
                }
                // Make new listing
                var newListing = new Transaction();

                newListing.Active       = 1;
                newListing.DateCreated  = DateTime.UtcNow;
                newListing.DateModified = DateTime.UtcNow;
                newListing.Description  = details.Description;
                newListing.Price        = details.ListPrice;
                newListing.ProductId    = pId;
                newListing.UserId       = details.UserId;

                db.Transactions.Add(newListing);

                // Add the images uploaded to the database
                foreach (var im in details.Images)
                {
                    var image = new Image();
                    image.ImageData = Convert.FromBase64String(im);
                    image.ProductId = pId;
                    image.UserId    = details.UserId;

                    db.Images.Add(image);
                }

                db.SaveChanges();

                Dictionary <string, string> r = new Dictionary <string, string>();
                r.Add("NewListingId", newListing.Id.ToString());
                response = JsonConvert.SerializeObject(r);
            }
            return(response);
        }
Exemplo n.º 5
0
        public static string Update(UpdateUser details)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                // Get the user we want to edit
                var uToChange = (from b in db.Users
                                 where b.Id == details.UIdToChange
                                 select b).FirstOrDefault();

                // If the hashed password equals the stored hash, then we are good to update info
                if (uToChange.PasswordHash == details.PasswordHash)
                {
                    if (details.NewPwHash != null && details.NewPwHash != "")
                    {
                        uToChange.PasswordHash = details.NewPwHash;
                    }
                }


                if (details.EmailAddress != null && details.EmailAddress != "")
                {
                    uToChange.EmailAddress = details.EmailAddress;
                }

                if (details.PhoneNumber != null && details.PhoneNumber != "")
                {
                    uToChange.PhoneNumber = details.PhoneNumber;
                }

                if (details.FirstName != null && details.FirstName != "")
                {
                    uToChange.FirstName = details.FirstName;
                }

                if (details.LastName != null && details.LastName != "")
                {
                    uToChange.LastName = details.LastName;
                }

                db.SaveChanges();
            }

            r.Status = 1;
            r.Data.Add("Result", "Operation Complete");
            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Searches the database for the specified criteria. If none are found, the list will be empty.
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        public static List <ListingDetails> SearchTransactions(ProductDetails details)
        {
            List <ListingDetails> tDetails   = new List <ListingDetails>();
            List <int>            productIds = new List <int>();

            using (var db = new TBDataModel())
            {
                var products = from b in db.Products
                               where b.ISBN.Contains(details.ISBN) ||
                               b.Title.Contains(details.Title) ||
                               b.Author.Contains(details.Author)
                               select b;

                foreach (var item in products)
                {
                    productIds.Add(item.Id);
                }

                var transactionList = from t in db.Transactions
                                      where productIds.Any(x => t.ProductId == x)
                                      select t;


                foreach (var item in transactionList)
                {
                    tDetails.Add(new ListingDetails
                    {
                        TransactionId = item.Id,
                        UserId        = item.UserId,
                        Name          = item.Product.Title,
                        Author        = item.Product.Author,
                        Publisher     = item.Product.Publisher,
                        ListPrice     = item.Price,
                        Negotiable    = item.Negotiable,
                        ISBN          = item.Product.ISBN,
                        Description   = item.Description
                    });
                }
            }

            return(tDetails);
        }
Exemplo n.º 7
0
        public static Dictionary <string, dynamic> Get(int tId)
        {
            Dictionary <string, dynamic> returnData = new Dictionary <string, dynamic>();

            using (var db = new TBDataModel())
            {
                var transaction = (from t in db.Transactions
                                   where t.Id == tId
                                   select t).FirstOrDefault();
                var            innerProduct = transaction.Product;
                ListingDetails lDetails     = new ListingDetails();
                lDetails.Author     = innerProduct.Author;
                lDetails.Name       = innerProduct.Title;
                lDetails.ListPrice  = transaction.Price;
                lDetails.ISBN       = innerProduct.ISBN;
                lDetails.Negotiable = transaction.Negotiable;
                lDetails.Publisher  = innerProduct.Publisher;
                lDetails.UserId     = transaction.UserId;

                // Get images
                var images = from b in db.Images
                             where b.ProductId == innerProduct.Id
                             select b;
                List <string> imageB64 = new List <string>();
                foreach (var im in images)
                {
                    imageB64.Add(Convert.ToBase64String(im.ImageData));
                }
                lDetails.Images = imageB64;

                BasicUserData bud = new BasicUserData();

                bud.UserName    = transaction.User.UserName;
                bud.PhoneNumber = transaction.User.PhoneNumber;

                returnData.Add("TransactionData", lDetails);
                returnData.Add("UserData", bud);
            }

            return(returnData);
        }
Exemplo n.º 8
0
        public static string Accept(AcceptPurchase details)
        {
            ResponseData r = new ResponseData();

            // Seller marked a sale as complete
            // API sets the transaction as inactive
            using (var db = new TBDataModel())
            {
                var chosenBid = (from b in db.Bids
                                 where b.Id == details.BidId
                                 select b).FirstOrDefault();


                // If the sale doesnt exist, then it doesnt exist
                if (chosenBid.Transaction == null || chosenBid.Transaction.Active == 0)
                {
                    throw new Exception("Transaction does not exist.");
                }

                // if the sale exists, set the sale to inactive to mark as complete
                chosenBid.Transaction.Active       = 0;
                chosenBid.Transaction.DateModified = DateTime.UtcNow;

                // set Accepted flag to true the bids associated with the item
                // But only if there are no others
                if (db.Bids.Any(x => x.Accepted == 1 && x.TransactionId == chosenBid.TransactionId))
                {
                    // if a bid has already been accepted
                    throw new Exception("There is already a bid accepted for this transaction.");
                }
                chosenBid.Accepted = 1;

                db.SaveChanges();
                r.Status = 1;
                r.Schema = "AcceptPurchase";
                r.Data.Add("Purchase Status", "Accepted");
            }

            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a new user based on the passed in 'details' parameter
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        public static string User(NewUser details)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                // Check to see if the username already exists. If it does, return an error
                var usernameId = (from b in db.Users
                                  where b.UserName == details.UserName
                                  select b.Id).FirstOrDefault();
                // If this evaluates to true, then the username is already in use
                if (usernameId != 0)
                {
                    Dictionary <string, string> error = new Dictionary <string, string>();
                    error.Add("Error", "Username already exists.");
                    error.Add("ErrorCode", "1");
                    return(JsonConvert.SerializeObject(error));
                }

                // If the username is okay, add data to db
                User newUser = new User();
                newUser.PasswordHash = details.PasswordHash;
                newUser.PhoneNumber  = details.PhoneNumber;
                newUser.UserName     = details.UserName;
                newUser.EmailAddress = details.EmailAddress;

                db.Users.Add(newUser);
                // Save user Changes
                db.SaveChanges();

                r.Status = 1;
                r.Schema = "NewUser";
                r.Data.Add("UserId", newUser.Id.ToString());
                r.Data.Add("UserName", newUser.UserName);
            }

            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 10
0
        public static string Make(BidData details)
        {
            ResponseData r = new ResponseData();

            using (var db = new TBDataModel())
            {
                // using the bid details
                // Add new bid to database
                EntityFramework.Bid newBid = new EntityFramework.Bid();
                newBid.ProposedPrice = details.ProposedPrice;
                newBid.TransactionId = details.TransactionId;
                newBid.UserId        = details.PurchaserId;
                newBid.Accepted      = 0;

                db.Bids.Add(newBid);
                db.SaveChanges();

                r.Status = 1;
                r.Schema = "NewBid";
                r.Data.Add("BidId", newBid.Id.ToString());
            }

            return(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 11
0
        public static string Auth(LoginDetails details)
        {
            Dictionary <string, string> rDict = new Dictionary <string, string>();

            try
            {
                using (TBDataModel db = new TBDataModel())
                {
                    // Check to see if the user-password combination exists

                    var uId = (from b in db.Users
                               where
                               b.PasswordHash == details.PasswordHash &&
                               b.UserName == details.UserName
                               select b.Id).FirstOrDefault <int>();

                    // if the UserId is -1 then the value doesnt exist
                    if (uId == 0)
                    {
                        rDict.Add("Status", "0");
                        rDict.Add("UserId", "Null");
                        return(JsonConvert.SerializeObject(rDict));
                    }

                    // Generate Json Token?

                    rDict.Add("Status", "1");
                    rDict.Add("UserId", uId.ToString());
                    return(JsonConvert.SerializeObject(rDict));
                }
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }