Example #1
0
        public void ValidateInput_Not_Empty()
        {
            ToDo toDoITem = new ToDo {
                SlNo = 1, Item = "Item1", Description = "Desc Item1"
            };
            List <Error> lstValidateInput = ValidateUtility.ValidateInput(toDoITem);

            Assert.IsTrue(lstValidateInput.Count == 0);
        }
Example #2
0
        public void ValidateInput_Description_Empty()
        {
            ToDo toDoITem = new ToDo {
                SlNo = 1, Item = "Item1", Description = ""
            };
            List <Error> lstValidateInput = ValidateUtility.ValidateInput(toDoITem);

            Assert.IsTrue(lstValidateInput.Count == 1);
            Assert.AreEqual(AppConstants.DESCRIPTION_EMPTY, lstValidateInput[0].errorCode);
            Assert.AreEqual(AppConstants.DESCRIPTION_EMPTY_MSG, lstValidateInput[0].errorMessage);
        }
Example #3
0
        [HttpPost] // Routing by explicitly specifying the HTTP Action type in the header
        public TodoResponse AddNewToDo(ToDo toDo)
        {
            TodoResponse   todoResponse   = new TodoResponse();
            ResponseHeader responseHeader = new ResponseHeader();
            ResponseBody   responseBody   = new ResponseBody();
            HttpStatusCode statusCode;
            List <Error>   errorlist     = new List <Error>();
            string         statusMessage = string.Empty;

            errorlist = ValidateUtility.ValidateInput(toDo);

            try
            {
                if (!ModelState.IsValid)
                {
                    statusCode    = HttpStatusCode.BadRequest;
                    statusMessage = AppConstants.Error;
                    errorlist.Add(new Error(AppConstants.MODAL_STATE_INVALID, AppConstants.BAD_REQUEST));
                }

                //Check if any of the input parameters are empty
                else if (errorlist.Count > 0)
                {
                    statusCode    = HttpStatusCode.BadRequest;
                    statusMessage = AppConstants.Error;
                    errorlist.ForEach(e => Logger.WriteLog(e.errorMessage));
                }

                else
                {
                    // Check if the item with same descrition is already present
                    if (ValidateUtility.ToDoExists(_repository.getAllTodo(), toDo))
                    {
                        statusCode    = HttpStatusCode.Conflict;
                        statusMessage = AppConstants.Error;
                        Error error = new Error(AppConstants.TODO_ALREADY_EXIST_MSG, AppConstants.TODO_ALREADY_EXIST);
                        errorlist.Add(error);
                        Logger.WriteLog(AppConstants.TODO_ALREADY_EXIST_MSG);
                    }

                    //If item not present, then insert new data
                    else
                    {
                        // return value and check the status and handle error
                        // Database insertion takes place here
                        if (_repository.insert(toDo) > 0)
                        {
                            statusCode    = HttpStatusCode.OK;
                            statusMessage = AppConstants.Success;
                        }

                        // If database insertion failed then handle the exception
                        else
                        {
                            statusCode    = HttpStatusCode.InternalServerError;
                            statusMessage = AppConstants.Error;
                            errorlist.Add(new Error(AppConstants.INSERT_FAILED_MSG, AppConstants.INSERT_FAILED));
                        }
                    }
                }
            }
            catch (DbUpdateException dbEx)
            {
                //If To Do item is already present in database with same id
                if (ToDoIdExists(toDo.SlNo))
                {
                    statusCode    = HttpStatusCode.Conflict;
                    statusMessage = AppConstants.Error;
                    string strErrorMsg = AppConstants.Error_Message + dbEx.Message + AppConstants.Inner_Exception + dbEx.InnerException +
                                         AppConstants.Stack_Trace + dbEx.StackTrace + AppConstants.Soure + dbEx.Source;
                    Error error = new Error(AppConstants.TODO_ALREADY_EXIST_MSG, AppConstants.TODO_ALREADY_EXIST);
                    errorlist.Add(error);
                    Logger.WriteLog(strErrorMsg);
                }
                else
                {
                    statusCode    = HttpStatusCode.InternalServerError;
                    statusMessage = AppConstants.Error;
                    string strErrorMsg = AppConstants.Error_Message + dbEx.Message;
                    errorlist.Add(new Error(AppConstants.INTERNAL_SERVER_ERROR_MSG, AppConstants.INTERNAL_SERVER_ERROR));
                    Logger.WriteLog(AppConstants.Error_Message + strErrorMsg + AppConstants.Inner_Exception + dbEx.InnerException +
                                    AppConstants.Stack_Trace + dbEx.StackTrace + AppConstants.Soure + dbEx.Source);
                }
            }

            //Write to log file if error is present
            if (errorlist.Count > 0)
            {
                //foreach (var error in errorlist)
                //{
                //    Logger.WriteLog(AppConstants.Error + " in PostToDo() " +AppConstants.Error_Code
                //                   + error.errorCode + AppConstants.Error_Message + error.errorMessage);
                //}
                errorlist.ForEach(error => Logger.WriteLog(AppConstants.Error + " in PostToDo() " + AppConstants.Error_Code
                                                           + error.errorCode + AppConstants.Error_Message + error.errorMessage));
            }
            responseHeader.statusCode    = statusCode;
            responseHeader.statusMessage = statusMessage;
            responseHeader.error         = errorlist;
            todoResponse.responseHeader  = responseHeader;
            responseBody.todo            = _repository.getAllTodo();
            todoResponse.responseBody    = responseBody;
            return(todoResponse);
        }
Example #4
0
        public TodoResponse PutToDo(int id, ToDo toDo)
        {
            TodoResponse   todoResponse   = new TodoResponse();
            ResponseHeader responseHeader = new ResponseHeader();
            ResponseBody   responseBody   = new ResponseBody();
            List <Error>   errorlist      = new List <Error>();
            HttpStatusCode statusCode;
            string         statusMessage = string.Empty;

            errorlist = ValidateUtility.ValidateInput(toDo);

            try
            {
                if (!ModelState.IsValid)
                {
                    statusCode    = HttpStatusCode.BadRequest;
                    statusMessage = AppConstants.Error;
                    errorlist.Add(new Error(AppConstants.MODAL_STATE_INVALID, AppConstants.BAD_REQUEST));
                }

                //Check if any of the input parameters are empty
                else if (errorlist.Count > 0)
                {
                    statusCode    = HttpStatusCode.BadRequest;
                    statusMessage = AppConstants.Error;
                    //add mulitple error if needed to errorlist
                    errorlist.ForEach(e => Logger.WriteLog(e.errorMessage));
                }

                // check if the ids in the request match
                else if (id != toDo.SlNo)
                {
                    statusCode    = HttpStatusCode.BadRequest;
                    statusMessage = AppConstants.Error;
                    //add mulitple error if needed to errorlist
                    errorlist.Add(new Error(AppConstants.TODO_ID_MISMATCH_MSG, AppConstants.TODO_ID_MISMATCH));
                }

                // Perform the Edit operation
                else
                {
                    // Database update
                    int result = _repository.save(toDo);
                    // return value and check the status and handle error
                    if (result > 0)
                    {
                        statusCode    = HttpStatusCode.OK;
                        statusMessage = AppConstants.Success;
                    }
                    else
                    {
                        statusCode    = HttpStatusCode.InternalServerError;
                        statusMessage = AppConstants.Error;
                        errorlist.Add(new Error(AppConstants.SAVE_FAILED_MSG, AppConstants.SAVE_FAILED));
                    }
                }
            }
            catch (DbUpdateConcurrencyException dbEx)
            {
                // If the Id for Deletion is not present in the database
                if (!ToDoIdExists(id))
                {
                    statusCode    = HttpStatusCode.NotFound;
                    statusMessage = AppConstants.Error;
                    string strErrorMsg = AppConstants.Error_Message + dbEx.Message;
                    errorlist.Add(new Error(strErrorMsg, AppConstants.TODO_NOT_FOUND));

                    Logger.WriteLog(AppConstants.Error_Message + strErrorMsg +
                                    AppConstants.Inner_Exception + dbEx.InnerException +
                                    AppConstants.Stack_Trace + dbEx.StackTrace + AppConstants.Soure + dbEx.Source);
                }
                else
                {
                    // This will be caught and handled in the ToDoCustomException handler
                    throw;
                }
            }

            //Write to log file if error is present
            if (errorlist.Count > 0)
            {
                errorlist.ForEach(error => Logger.WriteLog(AppConstants.Error + " in PutToDo() for ID : " + id + AppConstants.Error_Code
                                                           + error.errorCode + AppConstants.Error_Message + error.errorMessage));
            }
            responseHeader.statusCode    = statusCode;
            responseHeader.statusMessage = statusMessage;
            responseHeader.error         = errorlist;
            todoResponse.responseHeader  = responseHeader;
            responseBody.todo            = _repository.getAllTodo();
            todoResponse.responseBody    = responseBody;
            return(todoResponse);
        }