Пример #1
0
        public void ToDoExists_True()
        {
            ToDo toDoITem = new ToDo {
                SlNo = 1, Item = "Item1", Description = "Desc Item1"
            };
            MockTodoRepository mockObj    = new MockTodoRepository();
            List <ToDo>        getAllTodo = mockObj.getAllTodo();

            bool isExist = ValidateUtility.ToDoExists(getAllTodo, toDoITem);

            Assert.AreEqual(true, isExist);
        }
Пример #2
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);
        }