예제 #1
0
        public async ValueTask <IActionResult> Post([FromBody] BaseCheck model)
        {
            if (ModelState.IsValid)
            {
                var checker = await _repo.Item().Where(c => c.Name == model.Name && c.CategoryId == model.CategoryId).FirstOrDefaultAsync();

                if (checker != null)
                {
                    return(BadRequest(new ErrorDTO {
                        Message = "Check already exists"
                    }));
                }
                Check check = model.Convert <BaseCheck, Check>(_mapper);
                (bool succeeded, Check addedCheck, string error) = await _repo.Add(check);

                if (succeeded)
                {
                    return(Ok(addedCheck.Convert <Check, BaseCheck>(_mapper)));
                }
                return(BadRequest(new ErrorDTO {
                    Message = error
                }));
            }
            return(BadRequest(new ErrorDTO {
                Errors = ModelState.Values.SelectMany(e => e.Errors).ToList()
            }));
        }
예제 #2
0
        public async ValueTask <IActionResult> Put([FromBody] BaseCheck model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id != 0)
                {
                    Check check = model.Convert <BaseCheck, Check>(_mapper);
                    (bool succeeded, Check updatedCheck, string error) = await _repo.Update(check, model.Id);

                    if (succeeded)
                    {
                        return(Ok(updatedCheck.Convert <Check, BaseCheck>(_mapper)));
                    }
                    return(NotFound(new ErrorDTO {
                        Message = error
                    }));
                }
            }
            return(BadRequest(new ErrorDTO {
                Errors = ModelState.Values.SelectMany(e => e.Errors).ToList()
            }));
        }