Example #1
0
 public Task <ApiResult <ProductDto[]> > GetProducts()
 {
     return(Task.FromResult(ApiResult.Create(new[] {
         new ProductDto {
             Name = "商品1", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品2", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品3", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品4", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品5", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品6", Desc = "不知道怎么描述"
         },
         new ProductDto {
             Name = "商品7", Desc = "不知道怎么描述"
         },
     })));
 }
Example #2
0
        protected static async Task <IApiResult <IEnumerable <T> > > RetrieveCursoredResult <T>(IApiAccessor accessor,
                                                                                                Func <IApiAccessor, long, Task <IApiResult <ICursorResult <IEnumerable <T> > > > > func,
                                                                                                Action <Exception> exceptionHandler, CancellationToken token)
        {
            var resultList = new List <T>();

            CursorResultExtension.ApiContinuationReader <IEnumerable <T> > reader =
                () => accessor.ReadCursorApi(func, token);
            var lastRateLimit = RateLimitDescription.Empty;

            while (reader != null)
            {
                try
                {
                    var result = await reader().ConfigureAwait(false);

                    resultList.AddRange(result.Item1.Result);
                    lastRateLimit = result.Item1.RateLimit;
                    reader        = result.Item2;
                }
                catch (Exception ex)
                {
                    exceptionHandler(ex);
                    break;
                }
            }
            return(ApiResult.Create(resultList, lastRateLimit));
        }
Example #3
0
 protected ApiResult <T> Result <T>(Enum @enum, T data = default, string msg = null)
 {
     if (msg == null)
     {
         msg = I18n.GetText(@enum);
     }
     return(ApiResult <T> .Create(@enum.GetHashCode(), data, msg));
 }
Example #4
0
 protected ApiResult Result(Enum @enum, string msg = null)
 {
     if (msg == null)
     {
         msg = I18n.GetText(@enum);
     }
     return(ApiResult.Create(@enum.GetHashCode(), msg));
 }
Example #5
0
        public ApiResult <LoginResponseContract> Login(LoginRequestContract loginContract)
        {
            var result = new ApiResult <LoginResponseContract>();
            var data   = new LoginResponseContract()
            {
                AccessToken = "Access Token", RefreshToken = "Refresh Token"
            };

            return(result.Create("OK", EResponseCode.OK, data));
        }
 public static async Task <ApiResult <TResponse> > ApiAsync <TResponse>(this IServiceGateway client, IReturn <TResponse> requestDto, CancellationToken token = default(CancellationToken))
 {
     try
     {
         return(ApiResult.Create(await client.SendAsync <TResponse>((object)requestDto, token).ConfigAwait()));
     }
     catch (Exception e)
     {
         return(e.ToApiResult <TResponse>());
     }
 }
 public static async Task <ApiResult <List <TResponse> > > ApiAllAsync <TResponse>(this IServiceClientAsync client, List <IReturn <TResponse> > requestDtos, CancellationToken token = default(CancellationToken))
 {
     try
     {
         return(ApiResult.Create(await client.SendAllAsync <TResponse>(requestDtos, token).ConfigAwait()));
     }
     catch (Exception e)
     {
         return(e.ToApiResult <List <TResponse> >());
     }
 }
Example #8
0
        public async Task <ApiResult <UserInfo> > GetUserInfo()
        {
            var user = new UserInfo()
            {
                Name = "张三"
            };

            user.Address = (await GetAddress(user.ID)).Data;

            return(ApiResult.Create(user));
        }
 public static ApiResult <List <TResponse> > ApiAll <TResponse>(this IServiceGateway client, IEnumerable <IReturn <TResponse> > request)
 {
     try
     {
         return(ApiResult.Create(client.SendAll <TResponse>(request)));
     }
     catch (Exception ex)
     {
         return(ex.ToApiResult <List <TResponse> >());
     }
 }
 public static ApiResult <EmptyResponse> Api(this IServiceGateway client, IReturnVoid request)
 {
     try
     {
         client.Send <byte[]>(request);
         return(ApiResult.Create(new EmptyResponse()));
     }
     catch (Exception ex)
     {
         return(ex.ToApiResult());
     }
 }
Example #11
0
 public Task <ApiResult <Address[]> > GetAddress(Guid userID)
 {
     return(Task.FromResult(ApiResult.Create(new[] {
         new Address()
         {
             Province = "广东",
             City = "深圳"
         }, new Address()
         {
             Province = "我的老家",
             City = "这个屯"
         },
     })));
 }
 public static async Task <ApiResult <EmptyResponse> > ApiAsync(this IServiceGateway client, IReturnVoid requestDto, CancellationToken token = default(CancellationToken))
 {
     try
     {
         await(client is IServiceGatewayAsync nativeAsync
             ? nativeAsync.SendAsync <byte[]>(requestDto, token)
             : Task.Factory.StartNew(() => client.Send <byte[]>(requestDto), token)).ConfigAwait();
         return(ApiResult.Create(new EmptyResponse()));
     }
     catch (Exception e)
     {
         return(e.ToApiResult());
     }
 }
Example #13
0
        public ApiResult <Order> CreateOrder(
            CreateOrderOptions options)
        {
            if (options == null)
            {
                return(new ApiResult <Order>(
                           StatusCode.BadRequest, "null options"));
            }

            var cresult = customer_
                          .GetCustomerById(options.CustomerId);

            if (!cresult.Success)
            {
                return(ApiResult <Order> .Create(cresult));
            }

            var order = new Order();

            foreach (var id in options.ProductIds)
            {
                var prodResult = product_
                                 .GetProductById(id);

                if (!prodResult.Success)
                {
                    return(ApiResult <Order> .Create(
                               prodResult));
                }

                order.TotalCost  = 0;
                order.TotalCost += prodResult.Data.Price;

                order.OrderProducts.Add(
                    new OrderProduct()
                {
                    Product = prodResult.Data
                });
            }

            context_.Add(order);
            cresult.Data.Orders.Add(order);
            context_.SaveChanges();

            return(ApiResult <Order> .CreateSuccessful(order));
        }
Example #14
0
        public async Task <ApiResult <OrderDto> > CreateOrder([FromBody] CreateOrderDto order)
        {
            foreach (var item in order.Details)
            {
                var stock = await _clientFactory.GetProxy <IProduct>().CheckStock(item.ProductID);

                if (stock == null || !stock.IsSuccess || !stock.Data)
                {
                    return(ApiResult.Error <OrderDto>("库存不足"));
                }
            }

            return(ApiResult.Create(new OrderDto()
            {
                Address = order.Address,
                Details = order.Details
            }));
        }
Example #15
0
        private static async Task <Tuple <IApiResult <IEnumerable <T> >, ApiContinuationReader <IEnumerable <T> > > > ReadCursorApi <T>(
            this IApiAccessor accessor, long cursor,
            Func <IApiAccessor, long, Task <IApiResult <ICursorResult <IEnumerable <T> > > > > reader,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var r = await reader(accessor, cursor).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            var cr = r.Result;
            var ir = ApiResult.Create(cr.Result, r.RateLimit);
            ApiContinuationReader <IEnumerable <T> > callback = null;

            if (cr.CanReadNext)
            {
                callback = () => ReadCursorApi(accessor, cr.NextCursor, reader, cancellationToken);
            }
            return(Tuple.Create(ir, callback));
        }
Example #16
0
 public static IActionResult Result(object data)
 {
     return(ApiResult.Create(data));
 }
Example #17
0
 public Task <ApiResult <bool> > CheckStock(Guid productID)
 {
     return(Task.FromResult(ApiResult.Create(_checkCount++ % 4 != 0)));
 }
Example #18
0
 public static IActionResult Result(IResult result)
 {
     return(ApiResult.Create(result));
 }