コード例 #1
0
        public IHttpActionResult Add(Book Model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Data can not be saved"));
                }

                BookCategory _category = _bookCategoryRepository.Get(Model.CategoryId);
                if (_category == null)
                {
                    return(NotFound());
                }

                if (_bookRepository.AlreadyExists(Model.ISBN))
                {
                    return(BadRequest("ISBN already assigned to a book"));
                }

                string _message;
                if (!_bookRepository.ValidateInput(Model, out _message))
                {
                    return(BadRequest(_message));
                }

                _bookRepository.Add(Model);
                _bookRepository.Save();

                return(Ok("Record Saved"));
            }
            catch (Exception) { return(BadRequest("Record can not be saved")); }
        }