public async Task <IActionResult> Update(PromotionUpdateRequest request, [FromRoute] int id)
        {
            if (ModelState.IsValid)
            {
                var result = await _promotionServive.Update(request, id);

                if (result.IsSuccessed == true)
                {
                    TempData["result"]    = "Update Success";
                    TempData["IsSuccess"] = true;
                }
                else
                {
                    TempData["result"]    = result.Message;
                    TempData["IsSuccess"] = false;
                }
                return(RedirectToAction("Index", "promotion"));
            }
            else
            {
                TempData["IsSuccess"] = false;
                TempData["result"]    = string.Join(" | ", ModelState.Values
                                                    .SelectMany(v => v.Errors)
                                                    .Select(e => e.ErrorMessage));
                return(RedirectToAction("Index", "promotion"));
            }
        }
        public async Task <ApiResult <bool> > Update(PromotionUpdateRequest request, int promotionId)
        {
            var promotion = await _context.Promotions.FindAsync(promotionId);

            if (promotion == null)
            {
                return(new ApiResultErrors <bool>($"Can not find promotion with id: {promotionId}"));
            }
            promotion.DiscountAmount = request.DiscountAmount;
            promotion.FromDate       = request.FromDate;
            promotion.ToDate         = request.ToDate;
            return(await SaveChangeService.SaveChangeAsyncNotImage(_context));
        }
        public async Task <IActionResult> Update(PromotionUpdateRequest request, int promotionId)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }
            var result = await _promotionService.Update(request, promotionId);

            if (result.IsSuccessed == false)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Exemple #4
0
        public ActionResult PutPromotion(int id, PromotionUpdateRequest entity)
        {
            if (id != entity.Id)
            {
                return(BadRequest());
            }
            bool success = _proSer.Update(entity);

            if (success)
            {
                var updated = _proSer.GetById(entity.Id);
                return(Ok(updated));
            }
            return(Problem("Update failed!"));
        }
        public bool Update(PromotionUpdateRequest entity)
        {
            string   description = entity.Description;
            DateTime beginDate   = entity.BeginDate;
            DateTime expiredDate = entity.ExpiredDate;

            Promotion existed = _proRepo.GetById(entity.Id);

            if (existed == null)
            {
                return(false);
            }

            if (description != null)
            {
                if (!util.ValidRangeLengthInput(description, 1, 250))
                {
                    return(false);
                }
                existed.Description = description;
            }
            if (beginDate.Millisecond > 0 && expiredDate.Millisecond > 0)
            {
                if (beginDate.CompareTo(expiredDate) >= 0)
                {
                    return(false);
                }
                existed.BeginDate   = beginDate;
                existed.ExpiredDate = expiredDate;
            }
            if (beginDate.Millisecond > 0 && expiredDate.Millisecond == 0)
            {
                if (beginDate.CompareTo(existed.ExpiredDate) >= 0)
                {
                    return(false);
                }
                existed.BeginDate = beginDate;
            }
            if (beginDate.Millisecond == 0 && expiredDate.Millisecond > 0)
            {
                if (existed.BeginDate.CompareTo(expiredDate) >= 0)
                {
                    return(false);
                }
                existed.ExpiredDate = expiredDate;
            }
            return(_proRepo.Update(existed));
        }
Exemple #6
0
        public async Task <ApiResult <string> > Update(PromotionUpdateRequest request, int promotionId)
        {
            var sections = _httpContextAccessor.HttpContext.Session.GetString("Token");

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sections);
            var json        = JsonConvert.SerializeObject(request);
            var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var response    = await _client.PatchAsync($"/api/promotions/{promotionId}", httpContent);

            var result = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <ApiResultSuccess <string> >(result));
            }
            return(JsonConvert.DeserializeObject <ApiResultErrors <string> >(result));
        }