public void ValidateSalary(decimal?salary, OperationResponseBase response, string filedname) { if (salary > ModelConstants.Teacher.MaxSalary || salary < ModelConstants.Teacher.MinSalary) { response.AddError("007", $"{filedname} value: {salary} don't accepted! Choose a value between {ModelConstants.Teacher.MinSalary} and {ModelConstants.Teacher.MaxSalary}"); } }
public void ValidateGender(char?gender, OperationResponseBase response) { if (!this.IsGenderValid(gender)) { response.AddError("005", $"Invalid gender value: {gender}! Gender must be 'M' or 'F'"); } }
public void ValidatePageNumber(int?pageNumber, OperationResponseBase response) { if (pageNumber <= 0 || pageNumber == null) { response.AddError("002", $"Page number: {pageNumber} can't be null or zero."); } }
public void ValidateTeacherId(long?value, OperationResponseBase response) { if (value == null || value <= 0) { response.AddError("002", $"TeacherId can't be null or zero."); } }
private void EndTime(TimeSpan endTime, OperationResponseBase response, string lineWithError) { if (endTime < TimeSpan.FromHours(8)) { response.AddError("033", $"{lineWithError} The course can't end after 22:00"); } }
private void StartTime(TimeSpan startTime, OperationResponseBase response, string lineWithError) { if (startTime < TimeSpan.FromHours(8)) { response.AddError("032", $"{lineWithError} The course can't start before 8:00"); } }
public void ValidateSalary(decimal?minSalary, decimal?maxSalary, OperationResponseBase response) { if (minSalary != null && maxSalary != null) { this._parametersValidator.ValidateSalary(minSalary, response, nameof(minSalary)); this._parametersValidator.ValidateSalary(maxSalary, response, nameof(maxSalary)); if (maxSalary < minSalary) { response.AddError("019", $"{nameof(maxSalary)} can't be bigger than {nameof(minSalary)}"); } } else if (minSalary != null ^ maxSalary != null) { if (maxSalary != null) { this._parametersValidator.ValidateSalary(minSalary, response, nameof(minSalary)); } else if (maxSalary != null) { this._parametersValidator.ValidateSalary(maxSalary, response, nameof(maxSalary)); } } }
public void ValidateLevel(char?level, OperationResponseBase response) { IEnumerable <char> levels = this._levelRepository.ListAll(); if (!levels.Any(l => l == level)) { response.AddError("005", $"Invalid level value: {level}! Valid levels: {string.Join(", ",levels.ToArray())}"); } }
public void ClassId(int classId, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(classId, response, "Id")) { if (this._classRepository.ExistByClassId(classId) == true) { response.AddError("025", $"{lineWithError} ClassId already exist."); } } }
public void Teacher(string teacher, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(teacher, response, "Teacher")) { if (!this._teacherRepository.ExistByName(teacher)) { response.AddError("026", $"{lineWithError} Invalid teacher."); } } }
public void Course(string course, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(course, response, "Course")) { if (!this._courseRepository.ExistByName(course)) { response.AddError("027", $"{lineWithError} Invalid course."); } } }
public void Date(DateTime startDate, DateTime endDate, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(startDate, response, "StartDate") && !this._isNullOrZeroValidator.Execute(endDate, response, "EndDate")) { if (startDate > endDate) { response.AddError("030", $"{lineWithError} StartDate can't be bigger than EndDate"); } } }
public void ValidateAdmitionDate(DateTime?admitionDate, OperationResponseBase response, string fieldname) { if (admitionDate == DateTime.MinValue || admitionDate == null) { response.AddError("002", $"{fieldname} value: {admitionDate} can't be null or zero"); } else if (admitionDate > DateTime.MinValue && admitionDate > DateTime.Today) { response.AddError("008", $"{fieldname} value: {admitionDate} can't be bigger than today"); } }
public bool Execute(string str, OperationResponseBase response, string fieldname) { if (!string.IsNullOrWhiteSpace(str)) { response.AddError("002", $"{fieldname} can't be null or zero."); return(true); } return(false); }
public bool Execute(DateTime?date, OperationResponseBase response, string fieldname) { if (date == null || date == DateTime.MinValue) { response.AddError("002", $"{fieldname} can't be null or zero."); return(true); } return(false); }
public bool Execute(TimeSpan?time, OperationResponseBase response, string fieldname) { if (time == null || time == TimeSpan.Zero) { response.AddError("002", $"{fieldname} can't be null or zero."); return(true); } return(false); }
public void ValidatePageSize(int?pageSize, OperationResponseBase response) { if (pageSize <= 0 || pageSize == null) { response.AddError("002", $"Page size: {pageSize} can't be null or zero."); } else if (pageSize > ModelConstants.Teacher.MaxTeachersPerPage) { response.Errors.Add(new OperationError("015", $"Page size value: {pageSize} exceeded the limit.")); } }
public bool Execute(int?num, OperationResponseBase response, string fieldname) { if (num == 0 || num == null) { response.AddError("002", $"{fieldname} can't be null or zero."); return(true); } return(false); }
/// <summary> /// This method builds the HTTP response . /// </summary> /// <typeparam name="TResponseData"></typeparam> /// <param name="response"></param> /// <returns></returns> public static JsonResult BuildHttpResponse <TResponseData>(this OperationResponseBase <TResponseData> response) where TResponseData : OperationResponseData { // BadRequest Status Code. if (response.HttpStatusCode == HttpStatusCode.BadRequest) { response.SetBadRequestError(); return(new JsonResult(response) { StatusCode = (int)HttpStatusCode.BadRequest }); } // Unauthorized Status Code. if (response.HttpStatusCode == HttpStatusCode.Unauthorized) { response.SetUnauthorizedError(); return(new JsonResult(response) { StatusCode = (int)HttpStatusCode.Unauthorized }); } // InternalServerError Status Code. if (response.HttpStatusCode == HttpStatusCode.InternalServerError) { response.SetInternalServerError(); return(new JsonResult(response) { StatusCode = (int)HttpStatusCode.InternalServerError }); } // NotImplemented Status Code. if (response.HttpStatusCode == HttpStatusCode.NotImplemented) { response.SetNotImplementedError(); return(new JsonResult(response) { StatusCode = (int)HttpStatusCode.NotImplemented }); } response.SetSuccessCreated(); return(new JsonResult(response) { StatusCode = (int)HttpStatusCode.Created }); }
public void ValidateName_Should_WorkCorrectly(string input, bool isValid, int errorCount) { // Arrange OperationResponseBase response = new OperationResponseBase(); // Act this._validator.ValidateName(input, response); // Assert response.Success.Should().Be(isValid); response.Errors.Count.Should().Be(errorCount); }
public void ValidateTeacherId_Should_AddError_When_ReceiveNullOrZero_And_DoNothing_Otherwise(long?input, bool isValid) { // Arrange OperationResponseBase responseBase = new OperationResponseBase(); int errorCount = isValid ? 0 : 1; // Act this._validator.ValidateTeacherId(input, responseBase); // Assert responseBase.Success.Should().Be(isValid); responseBase.Errors.Count.Should().Be(errorCount); }
public void Shift(string shift, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(shift, response, "Shift")) { if (!char.IsUpper(shift[0])) { response.AddError("028", $"{lineWithError} First letter of shift must be in uppercase."); } else if (shift != "Manhã" && shift != "Tarde" && shift != "Noite") { response.AddError("029", $"{lineWithError} Invalid Shift! Valid shifts: Manhã, Tarde, Noite"); } } }
public void ValidateAdmitionDate_Should_HaveNoError_When_Between1000And10000_And_AddError_Otherwise(decimal?input, bool isValid) { // Arrange int errorCount = isValid ? 0 : 1; OperationResponseBase responseBase = new OperationResponseBase(); // Act this._validator.ValidateSalary(input, responseBase, "Teste"); // Assert responseBase.Success.Should().Be(isValid); responseBase.Errors.Count.Should().Be(errorCount); }
public void ValidateGender_Should_HaveNoError_When_FOrM_And_AddError_Otherwise(char?input, bool isValid) { // Arrange int errorCount = isValid ? 0 : 1; OperationResponseBase responseBase = new OperationResponseBase(); // Act this._validator.ValidateGender(input, responseBase); // Assert responseBase.Success.Should().Be(isValid); responseBase.Errors.Count.Should().Be(errorCount); }
public void ValidateLevelIds(List <char> levelIds, OperationResponseBase response) { if (levelIds.Count > 0) { List <char> validLevels = this._levelRepository.ListAll().ToList(); List <char> invalidLevels = levelIds.Except(validLevels).ToList(); if (invalidLevels.Count > 0) { response.AddError("017", $"Error in levelIds. Invalid values: {string.Join(", ", invalidLevels)}."); } } }
public void ValidateGenders(List <char> genders, OperationResponseBase response) { if (genders.Count > 0) { List <char> validGenders = new List <char> { 'M', 'F' }; List <char> invalidGenders = genders.Except(validGenders).ToList(); if (invalidGenders.Count > 0) { response.AddError("017", $"Error in levelIds. Invalid values: {string.Join(", ", invalidGenders)}."); } } }
public void Time(TimeSpan startTime, TimeSpan endTime, OperationResponseBase response, string lineWithError) { if (!this._isNullOrZeroValidator.Execute(startTime, response, "StartTime") && !this._isNullOrZeroValidator.Execute(endTime, response, "EndTime")) { StartTime(startTime, response, lineWithError); EndTime(endTime, response, lineWithError); if (startTime > endTime) { response.AddError("031", $"{lineWithError} StartTime can't be bigger than EndTime"); } else if (endTime - startTime > TimeSpan.FromHours(3)) { response.AddError("034", $"{lineWithError} Duration can't be bigger than 3h"); } } }
public void ValidateName(string name, OperationResponseBase response) { if (string.IsNullOrWhiteSpace(name)) { response.AddError("002", $"Name can't be null or empty."); return; } if (name.Length > ModelConstants.Teacher.NameMaxLength) { response.AddError("004", $"Invalid name length: {name.Length} exceeded the limit! Max: {ModelConstants.Teacher.NameMaxLength}."); } if (name.Any(c => char.IsSymbol(c)) || name.Any(c => char.IsNumber(c))) { response.AddError("020", $"Name can't have specials characters."); } }
public void ValidateTeacher(Teacher teacher, OperationResponseBase response) { if (teacher == null) { response.AddError("001", "Request can't be null"); return; } this._validator.ValidateTeacherId(teacher.TeacherId, response); this._validator.ValidateName(teacher.Name, response); this._validator.ValidateGender(teacher.Gender, response); this._validator.ValidateLevel(teacher.LevelId, response); this._validator.ValidateSalary(teacher.Salary, response, nameof(teacher.Salary)); this._validator.ValidateAdmitionDate(teacher.AdmitionDate, response, nameof(teacher.AdmitionDate)); }
public void ValidateAdmitionDate(DateTime?minAdmitionDate, DateTime?maxAdmitionDate, OperationResponseBase response) { if (minAdmitionDate != null && maxAdmitionDate != null) { this._parametersValidator.ValidateAdmitionDate(minAdmitionDate, response, nameof(minAdmitionDate)); this._parametersValidator.ValidateAdmitionDate(maxAdmitionDate, response, nameof(maxAdmitionDate)); if (maxAdmitionDate < minAdmitionDate) { response.AddError("019", $"{nameof(maxAdmitionDate)} can't be bigger than {nameof(minAdmitionDate)}"); } } else if (minAdmitionDate != null ^ maxAdmitionDate != null) { if (minAdmitionDate != null) { this._parametersValidator.ValidateAdmitionDate(minAdmitionDate, response, nameof(minAdmitionDate)); } else if (maxAdmitionDate != null) { this._parametersValidator.ValidateAdmitionDate(maxAdmitionDate, response, nameof(maxAdmitionDate)); } } }