Пример #1
0
        public void Test1(
            HttpRequest request,
            HttpResponse response,
            IPathParams pathParams,
            string pathParam1,
            int?pathParam2,
            IQueryParams queryParams,
            IFormParams formParams,
            IFormFileParams files,
            IFormFile uplaod,
            [ReadJson]
            JsonObject jsonObject
            )
        {
            Console.WriteLine($"HttpRequest:{request}");
            Console.WriteLine($"HttpResponse:{response}");
            Console.WriteLine($"IPathParams:{pathParams}");
            Console.WriteLine(pathParam1);
            Console.WriteLine(pathParam2);
            Console.WriteLine($"IQueryParams:{queryParams}");
            Console.WriteLine($"IFormParams:{formParams}");
            Console.WriteLine($"IFormFileParams:{files}");
            Console.WriteLine($"IFormFile:{uplaod}");
            Console.WriteLine($"JsonObject:{jsonObject}");

            response.UseCookie("CookieName", "CookieValue");
            response.UseRedirect("/");
        }
        public async Task <JsonData> Books(IFormParams formParams)
        {
            var isbn  = formParams.GetValue <string>("isbn");
            var name  = formParams.GetValue <string>("name");
            var page  = formParams.GetValue <int>("page");
            var limit = formParams.GetValue <int>("limit");
            var field = formParams.GetValue <string>("field");
            var order = formParams.GetValue <string>("order");

            //var where = Db.Where<Book>();
            //where = where.AndIf(!string.IsNullOrEmpty(isbn), (b, s) => b.ISBN.Contains(isbn));//不支持通配符,not support wildcard
            //where = where.AndIf(!string.IsNullOrEmpty(name), (b, s) => s.Like(b.Name, $"%{name}%")));

            var where = Db.Where <Book>()
                        .AndIf(!string.IsNullOrEmpty(isbn), (b, s) => b.ISBN.Contains(isbn))//better
                        .AndIf(!string.IsNullOrEmpty(name), (b, s) => s.Like(b.Name, $"%{name}%"));

            var orderBy = Db.OrderBy <Book>();

            if (order == "asc")
            {
                if (field == "CreateTime")
                {
                    orderBy = (b, s) => s.Asc(b.CreateTime);
                }
                else if (field == "Category.Name")
                {
                    orderBy = (b, s) => s.Asc(b.Category.Name);
                }
            }
            else if (order == "desc")
            {
                if (field == "CreateTime")
                {
                    orderBy = (b, s) => s.Desc(b.CreateTime);
                }
                else if (field == "Category.Name")
                {
                    orderBy = (b, s) => s.Desc(b.Category.Name);
                }
            }

            var offset = (page - 1) * limit;

            (var books, var count) = await Db.SelectPagedAsync(offset, limit, (b, s) => s.Navigate(b), where, orderBy);

            return(Json(0, "ok", count, books));
        }
Пример #3
0
        public async Task <JsonData> Login(IFormParams formParams)
        {
            var name     = formParams.GetValue <string>("name");
            var password = formParams.GetValue <string>("password");

            var account = await Db.SelectSingleAsync <Account>((a, s) => a, (a, s) => a.Name == name && a.Password == password);

            if (account == null)
            {
                return(Json(1101, "login error"));
            }

            var passport = new Passport(Response);

            passport.Id   = account.Id;
            passport.Name = account.Name;

            return(Json(0, "ok", "/Books"));
        }
Пример #4
0
        public JsonData Register(IFormParams formParams)
        {
            var name     = formParams.GetValue <string>("name");
            var password = formParams.GetValue <string>("password");

            if (string.IsNullOrEmpty(name))
            {
                return(Json(1001, "name is empty"));
            }
            if (string.IsNullOrEmpty(password))
            {
                return(Json(1002, "password is empty"));
            }

            //OR _Register.WaitAsync(name)
            if (_Register.TryWait(name))
            {
                try
                {
                    var count = Db.Select <Account, int>((a, s) => s.Count(), (a, s) => a.Name == name);
                    if (count > 0)
                    {
                        return(Json(1003, "name is exist"));
                    }

                    Db.Insert <Account>((s) => new Account()
                    {
                        Name     = name,
                        Password = password
                    });

                    return(Json(0, "register success"));
                }
                finally
                {
                    _Register.Realese(name);
                }
            }
            else
            {
                return(Json(1003, "server is busy"));
            }
        }
        public async Task <JsonData> Books1(IFormParams formParams)
        {
            var isbn  = formParams.GetValue <string>("isbn");
            var name  = formParams.GetValue <string>("name");
            var page  = formParams.GetValue <int>("page");
            var limit = formParams.GetValue <int>("limit");
            var field = formParams.GetValue <string>("field");
            var order = formParams.GetValue <string>("order");

            //var where = new Where<Book>();
            //where.AndIf(isbn, (b, s) => b.ISBN.Contains(isbn));
            ////where.AndIf(!string.IsNullOrEmpty(isbn), (b, s) => b.ISBN.Contains(isbn));
            //where.AndIf(name, (b, s) => s.Like(b.Name, $"%{name}%"));
            ////where.AndIf(!string.IsNullOrEmpty(name), (b, s) => s.Like(b.Name, $"%{name}%")));

            var where = new Where <Book>()
                        .AndIf(isbn, (b, s) => b.ISBN.Contains(isbn))//better
                        .AndIf(name, (b, s) => s.Like(b.Name, $"%{name}%"));

            var orderBy = _OrderBy[(field, order)];
Пример #6
0
 public QueryAndForm(IQueryParams queryParams, IFormParams formParams)
 {
     _queryParams = queryParams;
     _formParams  = formParams;
 }