public virtual IHttpActionResult SelectAll()
        {
            var funcName = "SelectAll";

            Logger.DebugFormat("{0} <-- Start", funcName);
            IHttpActionResult result;

            try
            {
                var records      = this.GetRecords();
                var totalRecords = this.GetTotalRecords();

                // Customize records if neccessary
                records = this.CustomizeRecords(records, ref totalRecords);

                // Serialize return data
                var responseContext = new SelectionResponseContext <T>
                {
                    Session         = this.selectionRequestContext.Session,
                    NumTotalRecords = totalRecords,
                    Records         = records,
                    Result          = true,
                    Description     = string.Empty
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
        public IHttpActionResult Delete(int id)
        {
            var funcName = "Delete";

            Logger.DebugFormat("{0} <-- Start", funcName);
            IHttpActionResult result;

            try
            {
                // Delete ProductCategory item in database
                this.DeleteRecord(id);

                // Commit changed
                this.UnitOfWork.SaveChanges((exception) =>
                {
                    throw new DatabaseException((int)DatabaseErrorCode.Delete, exception);
                });

                // Build action result
                var responseContext = new UpdateResponseContext <T>
                {
                    Result      = true,
                    Description = "Dữ liệu đã được xóa"
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
        public virtual IHttpActionResult SelectByID(int id)
        {
            var funcName = "SelectByID";

            Logger.DebugFormat("{0} <-- Start", funcName);
            Logger.DebugFormat("{0} - Input params: id={1}", funcName, id);
            IHttpActionResult result;

            try
            {
                // Get record by identifier from database
                var record = this.GetRecordById(id);

                // Serialize return data
                var responseContext = new SelectionResponseContext <T>
                {
                    Record      = record,
                    Result      = true,
                    Description = string.Empty
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
        public virtual IHttpActionResult Update(UpdateRequestContext <T> requestContext)
        {
            var funcName = "Update";

            Logger.DebugFormat("{0} <-- Start", funcName);
            IHttpActionResult result;

            try
            {
                if (requestContext == null)
                {
                    throw new ArgumentException("Could not get request context");
                }

                // Keeps input request for using later
                this.updateRequestContext = requestContext;

                // Update record to database
                var record = this.UpdateRecord();

                // Commit changed
                this.UnitOfWork.SaveChanges((exception) =>
                {
                    throw new DatabaseException((int)DatabaseErrorCode.Update, exception);
                });

                // Build action result
                var responseContext = new UpdateResponseContext <T>
                {
                    Record      = record,
                    Result      = true,
                    Description = "Dữ liệu đã được cập nhật"
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
        public virtual IHttpActionResult Select(SelectionRequestContext requestContext)
        {
            var funcName = "Select";

            Logger.DebugFormat("{0} <-- Start", funcName);
            IHttpActionResult result;

            try
            {
                if (requestContext == null)
                {
                    throw new ArgumentException("Could not get request context");
                }

                // Keeps input request for using later
                this.selectionRequestContext = requestContext;

                // Get datasource from database
                var records      = this.GetRecords();
                var totalRecords = this.GetTotalRecords();

                // Customize records if neccessary
                records = this.CustomizeRecords(records, ref totalRecords);

                // Serialize return data
                var responseContext = new SelectionResponseContext <T>
                {
                    Session         = this.selectionRequestContext.Session,
                    NumTotalRecords = totalRecords,
                    Records         = records,
                    Result          = true,
                    Description     = string.Empty
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
Exemple #6
0
        public virtual IHttpActionResult SelectByUserNameAndPassword(string username, string password)
        {
            var funcName = "SelectByUserNameAndPassword";

            Logger.DebugFormat("{0} <-- Start", funcName);
            Logger.DebugFormat("{0} - Input params: username={1}, password={2}", funcName, username, password);
            IHttpActionResult result;

            try
            {
                // Get record by identifier from database
                var records = this.UnitOfWork.UserDAO.Select(new EntityQueryArgs <User>
                {
                    StartRecordIndex  = this.selectionRequestContext.StartRecordIndex,
                    NumRecordsPerPage = this.selectionRequestContext.NumRecordsPerPage,
                    OrderByExpr       = this.UnitOfWork.UserDAO.BuildOrderByExpression(),
                    FilterExpr        = n => n.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) &&
                                        n.Password.Equals(password, StringComparison.OrdinalIgnoreCase)
                });
                var record = records.FirstOrDefault();

                // Serialize return data
                var responseContext = new SelectionResponseContext <User>
                {
                    Record      = record,
                    Result      = true,
                    Description = string.Empty
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
        public IHttpActionResult IsNotExist(string name)
        {
            var funcName = "IsNotExist";

            Logger.DebugFormat("{0} <-- Start", funcName);
            Logger.DebugFormat("{0} - input params: Name={0}", name);
            IHttpActionResult result;

            try
            {
                // True: not exist - False: otherwise
                var isExist = !this.IsExistRecord(name);
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, isExist);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }
Exemple #8
0
        public virtual IHttpActionResult Checkout(int id)
        {
            var funcName = "Checkout";

            Logger.DebugFormat("{0} <-- Start", funcName);
            Logger.DebugFormat("{0} - Input params: id={1}", funcName, id);
            IHttpActionResult result;

            try
            {
                /* STEP 1: Update table available */
                var table = this.GetRecordById(id);
                table.Status = (int)TableStatus.Available;
                this.UnitOfWork.TableDAO.Update(table);

                /* STEP 2: Get latest order with receiving state of current table, it will be created if not exist */
                var orderStates = this.UnitOfWork.OrderStateDAO.SelectAll();
                if (!orderStates.Any())
                {
                    throw new ArgumentException("Chưa thiết lập bất cứ trạng thái đơn hàng nào");
                }

                var orders = this.UnitOfWork.OrderDAO.Select(new EntityQueryArgs <Order>
                {
                    StartRecordIndex  = 0,
                    NumRecordsPerPage = 1,
                    OrderByExpr       = this.UnitOfWork.OrderDAO.BuildOrderByExpression(),
                    SortDirection     = "desc",
                    FilterExpr        = n => n.TableId.Equals(table.TableId) && n.OrderState.Name.Equals("Accept")
                });

                Order order = null;
                if (orders.IsNullOrEmpty()) // Order has created before
                {
                    throw new BusinessException($"Could not find order with receiving state of table [{table.TableId}]");
                }
                order = orders.First();
                order.OrderStateId = orderStates.Last().OrderStateId;
                this.UnitOfWork.OrderDAO.Update(order);

                // Commit changed
                this.UnitOfWork.SaveChanges((exception) =>
                {
                    throw new DatabaseException((int)DatabaseErrorCode.Insert, exception);
                });

                // Serialize return data
                var responseContext = new SelectionResponseContext <Table>
                {
                    Record      = table,
                    Result      = true,
                    Description = string.Empty
                };
                result = HttpActionResultBuilder.BuildJsonContentResult(this.Request, responseContext);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("{0} - Exception: {1}", funcName, ex);
                result = HttpActionResultBuilder.BuildExceptionResult(this.Request, ex);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }