//[ContentType("application/json")]
 public IActionResult AddCounter([FromBody] Counter counter)
 {
     if (_counterRepository.Add(counter) > 0)
     {
         return(Redirect("Index"));
     }
     else
     {
         return(View(counter));
     }
 }
Example #2
0
        public async Task <IActionResult> Post([FromBody] CounterState state)
        {
            try
            {
                await _counterRepository.Initialize();

                bool exists        = false;
                var  existingState = await _counterRepository
                                     .GetLast();

                if (existingState == null)
                {
                    existingState = new Repositories.CounterState();
                }
                else
                {
                    exists = true;
                }

                existingState.CreatedAt = DateTimeOffset.UtcNow;

                if (!state.Count.HasValue || Math.Abs(state.Count.Value - existingState.Count) > 10)
                {
                    existingState.Count += 1;
                }
                else
                {
                    existingState.Count = state.Count.Value;
                }

                if (exists)
                {
                    await _counterRepository.Update(existingState);
                }
                else
                {
                    await _counterRepository.Add(existingState);
                }

                return(Ok(new CounterState
                {
                    Count = existingState.Count,
                    CreatedAt = existingState.CreatedAt,
                    Store = _storeType
                }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public async Task <IActionResult> Create([FromBody] SaveCounterResource counterResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var counter = mapper.Map <SaveCounterResource, Counter>(counterResource);

            counterRepository.Add(counter);
            if (await unitOfWork.CompleteAsync() == false)
            {
                throw new Exception(message: $"Create New Counter failed on Save");
            }
            counter = await counterRepository.GetOne(counter.Id);

            var result = mapper.Map <Counter, ViewCounterResource>(counter);

            return(Ok(result));
        }