private async Task ValidateStudent(AddStudentBlContract contract, Guid?id = null) { if (contract == null) { throw new ArgumentNullException("contract"); } if (string.IsNullOrEmpty(contract.FirstName) || contract.FirstName.Length > 40) { throw new ArgumentException("This field is required and length must be less or equal 40", "FirstName"); } if (string.IsNullOrEmpty(contract.LastName) || contract.LastName.Length > 40) { throw new ArgumentException("This field is required and length must be less or equal 40", "LastName"); } if (!string.IsNullOrEmpty(contract.MiddleName) && contract.MiddleName.Length > 60) { throw new ArgumentException("Length must be less or equal 60", "MiddleName"); } if (!string.IsNullOrEmpty(contract.Uid)) { if (contract.Uid.Length < 6 || contract.Uid.Length > 16) { throw new ArgumentException("Length must be between 6 and 16", "uid"); } IQueryable <Student> filter(IQueryable <Student> query) { if (!string.IsNullOrEmpty(contract.Uid)) { query = query.Where(x => contract.Uid == x.Uid && x.Id != id); } return(query); } var count = await _studentDbService.GetStudentCount(filter); if (count > 0) { throw new ArgumentException("This field must be unique", "uid"); } } }
public async Task <Guid> AddStudent(AddStudentBlContract contract) { await ValidateStudent(contract); var newStudent = new Student { Deleted = false, FirstName = contract.FirstName, LastName = contract.LastName, MiddleName = contract.MiddleName, Sex = contract.Sex, Uid = contract.Uid, }; await _studentDbService.AddStudent(newStudent); return(newStudent.Id); }