Example #1
0
        public async Task <ActionResult> CreateOrUpdateAsync(string offerName, [FromBody] Offer offer)
        {
            AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);
            if (offer == null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(offer)), UserErrorCode.PayloadNotProvided);
            }

            if (!offerName.Equals(offer.OfferName))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(Offer).Name),
                                                      UserErrorCode.NameMismatch);
            }

            if (await _offerService.ExistsAsync(offerName))
            {
                _logger.LogInformation($"Update offer {offerName} with payload {JsonConvert.SerializeObject(offer)}");
                await _offerService.UpdateAsync(offerName, offer);

                return(Ok(offer));
            }
            else
            {
                _logger.LogInformation($"Create offer {offerName} with payload {JsonConvert.SerializeObject(offer)}");
                await _offerService.CreateAsync(offer);

                return(CreatedAtRoute(nameof(GetAsync) + nameof(Offer), new { offerName = offer.OfferName }, offer));
            }
        }
Example #2
0
    public static async Task <IResult> Create(int userid, int beanid, long quantity, decimal price, int days, string buysell, IOfferService offerService)
    {
        if (userid <= 0)
        {
            return(Results.BadRequest(new ApiError(string.Format(Strings.Invalid, "user id"))));
        }
        if (beanid <= 0)
        {
            return(Results.BadRequest(new ApiError(string.Format(Strings.Invalid, "bean id"))));
        }
        if (quantity <= 0 || price <= 0M || days <= 0)
        {
            return(Results.BadRequest(new ApiError(string.Format(Strings.GTZero, "Quantity, price and days"))));
        }
        if (!bool.TryParse(buysell, out var buy))
        {
            return(Results.BadRequest(new ApiError(string.Format(Strings.BuySell, buysell))));
        }
        var result = await offerService.CreateAsync(userid, beanid, quantity, price, DateTime.UtcNow.AddDays(days), buy);

        if (!result.Successful)
        {
            return(Results.BadRequest(new ApiError(result.ErrorMessage())));
        }
        return(Results.Ok());
    }
Example #3
0
        public virtual async Task <IHttpActionResult> Post(Offer offer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CancellationTokenSource token = new CancellationTokenSource();
            var register = await Service.CreateAsync(offer, token);

            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(JsonConvert.SerializeObject(register), Encoding.UTF8, "application/json");
            return(ResponseMessage(response));
        }