コード例 #1
0
ファイル: PostHandler.cs プロジェクト: hw3jung/Chanel
        public static int createPost(Post newPost)
        {
            int id = -1;

            try
            {
                DataAccess da = new DataAccess();

                Dictionary<string, object> post = new Dictionary<string, object>();
                post.Add("UserId", newPost.UserId);
                post.Add("TextBookId", newPost.TextBookId);
                post.Add("ActionBy ", (int) newPost.ActionBy); // enum must be casted to int before we can store it in db
                post.Add("Price", newPost.Price);
                post.Add("BookCondition", (int) newPost.BookCondition); // enum must be casted to int before we can store it in db
                post.Add("IsTransacting", newPost.IsTransacting);
                post.Add("IsActive", 1);
                post.Add("IsDeleted", 0);
                post.Add("CreatedDate", DateTime.Now);
                post.Add("ModifiedDate", DateTime.Now);

                id = da.insert(post, "Posts");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message + "    " + ex.StackTrace);
            }

            return id;
        }
コード例 #2
0
ファイル: Matchmaker.cs プロジェクト: hw3jung/Chanel
        /*
         * Given a new post, match it with the most appropriate post (if possible).
         *
         * Conditions
         * ----------
         *
         * If the new post was made by a buyer, then the matching post must have:
         *      -price less than or equal to that specified by buyer
         *      -book condition better than or equal to that specified by buyer
         *
         * If the new post was made by a seller, then the matching post must have:
         *      -price greater than or equal to that specified by seller
         *      -book condition worse than or equal to that specified by seller
         *
         */
        public static void Match(Post newPost)
        {
            if (!PostHandler.isPostAvailable(newPost.PostId))
            {
                return;
            }

            Post matchingPost = PostHandler.findMatchingPost(newPost);
            if (matchingPost != null)
            {
                int buyerPostId = -1;
                int sellerPostId = -1;

                int buyerUserId = -1;
                int sellerUserId = -1;

                int initialPrice;

                if (newPost.ActionBy == ActionBy.Buyer)
                {
                    buyerPostId = newPost.PostId;
                    sellerPostId = matchingPost.PostId;

                    buyerUserId = newPost.UserId;
                    sellerUserId = matchingPost.UserId;

                    initialPrice = matchingPost.Price;
                }
                else
                {
                    buyerPostId = matchingPost.PostId;
                    sellerPostId = newPost.PostId;

                    buyerUserId = matchingPost.UserId;
                    sellerUserId = newPost.UserId;

                    initialPrice = newPost.Price;
                }

                // Create transaction and transactionhistory for the matched buyer & seller
                Transaction newTransaction = new Transaction(
                    -1, // id doesnt matter here
                    newPost.TextBookId,
                    sellerUserId,
                    buyerUserId,
                    sellerPostId,
                    buyerPostId,
                    null,
                    null,
                    initialPrice,
                    0,
                    1,
                    0,
                    DateTime.Now,
                    DateTime.Now
                );

                PostHandler.updatePostState(newPost.PostId, 1);
                PostHandler.updatePostState(matchingPost.PostId, 1);

                int transactionId = TransactionHandler.CreateTransaction(newTransaction);
                newTransaction.TransactionId = transactionId;

                TransactionHandler.CreateTransactionHistory(transactionId, buyerUserId);
                TransactionHandler.CreateTransactionHistory(transactionId, sellerUserId);
            }
        }
コード例 #3
0
ファイル: QueueWorker.cs プロジェクト: hw3jung/Chanel
 public static void AddPost(Post newPost)
 {
     BookQueueSingleton.Instance.Add(newPost);
 }
コード例 #4
0
ファイル: PostController.cs プロジェクト: hw3jung/Chanel
        public ActionResult CreatePost(CreatePostModel model)
        {
            if (ModelState.IsValid)
            {
                int textbookId = model.TextBookId;

                // if we have a new textbook, store it
                if (model.IsNewBook)
                {
                    // proceed if course id exists; otherwise create the course first
                    Course course = CourseHandler.getCourseByName(model.CourseName);
                    if (course == null)
                    {
                        model.CourseId = CourseHandler.CreateCourse(model.CourseName);
                    }

                    var newTextbook = new Textbook(
                        -1, // id doesnt matter here
                        model.BookTitle,
                        model.ISBN,
                        model.Author,
                        model.CourseId,
                        model.CourseName,
                        model.BookImageUrl,
                        null,
                        1,
                        0,
                        DateTime.Now,
                        DateTime.Now
                    );

                    textbookId = TextbookHandler.createTextBook(newTextbook);
                }

                int profileId = ProfileHandler.GetProfileId(User.Identity.Name);
                int price = model.Price;
                ActionBy actionBy = model.ActionBy;

                if (model.IsNegotiable)
                {
                    if (actionBy == ActionBy.Buyer)
                    {
                        price = int.MaxValue;
                    }
                    else
                    {
                        price = 0;
                    }
                }

                var newPost = new Post(
                    -1, // id doesnt matter here
                    profileId,
                    textbookId,
                    actionBy,
                    price,
                    model.BookCondition,
                    0,
                    1,
                    0,
                    DateTime.Now,
                    DateTime.Now
                );

                int postId = PostHandler.createPost(newPost);
                newPost.PostId = postId;
                Task.Run(() => QueueWorker.AddPost(newPost));

                // TODO: redirect to special "you've successfully created post" page
                // with links to create another buy/sell post
                return RedirectToAction("Index", "Home");
            }

            // If we got this far, something failed, redisplay form
            IEnumerable<Textbook> textBookCollection = TextbookHandler.getAllTextbooks();

            // test data
            //for(int i = 0; i < 100; i++) {
            //    Textbook book = new Textbook(
            //        i,
            //        "Financial Accounting " + i,
            //        "100000000000" + i,
            //        "Author " + i,
            //        100 + i,
            //        "AFM 10" + i,
            //        null,
            //        10 + i,
            //        1,
            //        0,
            //        DateTime.Now,
            //        DateTime.Now
            //    );
            //    textBookCollection.Add(book);
            //}

            model.PostTypes = SelectListUtility.getPostTypes();
            model.BookConditions = SelectListUtility.getBookConditions();
            model.Textbooks = textBookCollection;

            return View("CreatePost", model);
        }
コード例 #5
0
ファイル: PostHandler.cs プロジェクト: hw3jung/Chanel
        public static Post findMatchingPost(Post post)
        {
            Post matchingPost = null;

            try
            {
                var counterparty = post.ActionBy == ActionBy.Buyer ? ActionBy.Seller : ActionBy.Buyer;

                string query = String.Format("UserId <> {0} AND TextBookId = {1} AND ActionBy = {2} " +
                    "AND IsTransacting = 0 AND IsActive = 1 AND IsDeleted = 0",
                    post.UserId,
                    post.TextBookId,
                    (int)counterparty
                );

                List<SortColumn> sortColumns = new List<SortColumn>();
                if (post.ActionBy == ActionBy.Buyer) //buyer
                {
                    query += String.Format("AND Price <= {0} AND BookCondition >= {1}",
                        post.Price,
                        (int)post.BookCondition
                    );

                    sortColumns.Add(new SortColumn("Price", "ASC"));
                }
                else //seller
                {
                    query += String.Format("AND Price >= {0} AND BookCondition <= {1}",
                        post.Price,
                        (int)post.BookCondition
                    );

                    sortColumns.Add(new SortColumn("Price", "DESC"));
                }

                DataAccess da = new DataAccess();
                DataTable dt = da.select(query, "Posts", NumRows : 1, SortColumns : sortColumns);

                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    int postId = Convert.ToInt32(row["PostId"]);
                    int profileId = Convert.ToInt32(row["UserId"]);
                    int textBookId = Convert.ToInt32(row["TextBookId"]);
                    ActionBy actionBy = (ActionBy)Convert.ToInt32(row["ActionBy"]);
                    int price = Convert.ToInt32(row["Price"]);
                    BookCondition bookCondition = (BookCondition)Convert.ToInt32(row["BookCondition"]);
                    int isTransacting = Convert.ToInt32(row["IsTransacting"]);
                    int isActive = Convert.ToInt32(row["IsActive"]);
                    int isDeleted = Convert.ToInt32(row["IsDeleted"]);
                    DateTime createdDate = (DateTime)row["CreatedDate"];
                    DateTime modifiedDate = (DateTime)row["ModifiedDate"];

                    matchingPost = new Post(
                        postId,
                        profileId,
                        textBookId,
                        actionBy,
                        price,
                        bookCondition,
                        isTransacting,
                        isActive,
                        isDeleted,
                        createdDate,
                        modifiedDate
                    );
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message + "   " + ex.StackTrace);
            }

            return matchingPost;
        }
コード例 #6
0
ファイル: PostHandler.cs プロジェクト: hw3jung/Chanel
        public static Post getPost(int postId)
        {
            Post post = null;

            try
            {
                DataAccess da = new DataAccess();
                DataTable dt = da.select(String.Format("PostId = '{0}' AND IsDeleted = 0", postId), "Posts", NumRows : 1);

                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    int profileId = Convert.ToInt32(row["UserId"]);
                    int textbookId = Convert.ToInt32(row["TextBookId"]);
                    ActionBy actionBy = (ActionBy)Convert.ToInt32(row["ActionBy"]);
                    int price = Convert.ToInt32(row["Price"]);
                    BookCondition bookCondition = (BookCondition)Convert.ToInt32(row["BookCondition"]);
                    int isTransacting = Convert.ToInt32(row["IsTransacting"]);
                    int isActive = Convert.ToInt32(row["IsActive"]);
                    int isDeleted = Convert.ToInt32(row["IsDeleted"]);
                    DateTime createdDate = (DateTime)row["CreatedDate"];
                    DateTime modifiedDate = (DateTime)row["ModifiedDate"];

                    post = new Post(
                        postId,
                        profileId,
                        textbookId,
                        actionBy,
                        price,
                        bookCondition,
                        isTransacting,
                        isActive,
                        isDeleted,
                        createdDate,
                        modifiedDate
                    );
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message + "   " + ex.StackTrace);
            }

            return post;
        }