//returns "MODEL.User" object if successful/found //returns "null" if not internal User findUserByUserName(string userName) { int result = (int)CODE.MINUS_ONE; User user = null; //validate userName if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(userName) || !Validate.isAlphaNumericWithUnderscore(userName) || !Validate.hasMinLength(userName, 5) || !Validate.hasMaxLength(userName, 15) ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { IUsers _DbUsers = new DbUsers(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { user = _DbUsers.findUserByUserName(userName); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || user == null) { return(null); } else { return(user); } }
//returns "MODEL.User" object if successful/found //returns "null" if not internal User findUserByEmail(string email) { int result = (int)CODE.MINUS_ONE; User user = null; //validate email if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(email) || !Validate.hasMinLength(email, 5) || !Validate.hasMaxLength(email, 50) || !email.Contains("@") ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { IUsers _DbUsers = new DbUsers(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { user = _DbUsers.findUserByEmail(email); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || user == null) { return(null); } else { return(user); } }
//returns "MODEL.Job" object if successful/found //returns "null" if not internal Job findJobByJobId(int jobId) { int result = (int)CODE.MINUS_ONE; Job job = null; //validate jobId if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(jobId.ToString()) || !Validate.isAllNumbers(jobId.ToString()) || !Validate.integerIsBiggerThan(jobId, (int)CODE.TRANSLATO_DATABASE_SEED - 1) ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { IJobs _DbJobs = new DbJobs(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { job = _DbJobs.findJobByJobId(jobId); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || job == null) { return(null); } else { return(job); } }
//returns "MODEL.Submission" object if successful/found //returns "null" if not internal Submission findSubmissionBySubmissionId(int submissionId) { int result = (int)CODE.MINUS_ONE; Submission submission = null; //validate submissionId if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(submissionId.ToString()) || !Validate.isAllNumbers(submissionId.ToString()) || !Validate.integerIsBiggerThan(submissionId, (int)CODE.TRANSLATO_DATABASE_SEED - 1) ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { ISubmissions _DbSubmission = new DbSubmissions(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { submission = _DbSubmission.findSubmissionBySubmissionId(submissionId); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || submission == null) { return(null); } else { return(submission); } }
//returns "MODEL.Upload" object if successful/found //returns "null" if not internal Upload findUploadByUploadId(int uploadId) { int result = (int)CODE.MINUS_ONE; Upload upload = null; //validate uploadId if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(uploadId.ToString()) || !Validate.isAllNumbers(uploadId.ToString()) || !Validate.integerIsBiggerThan(uploadId, (int)CODE.TRANSLATO_DATABASE_SEED - 1) ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { IUploads _DbUploads = new DbUploads(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { upload = _DbUploads.findUploadByUploadId(uploadId); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || upload == null) { return(null); } else { return(upload); } }
//returns "MODEL.Language" object if successful/found //returns "null" if not internal Language findLanguageByLanguageId(int languageId) { int result = (int)CODE.MINUS_ONE; Language language = null; //validate languageId if ( result == (int)CODE.ZERO || string.IsNullOrWhiteSpace(languageId.ToString()) || !Validate.isAllNumbers(languageId.ToString()) || !Validate.integerIsBiggerThan(languageId, (int)CODE.TRANSLATO_DATABASE_SEED - 1) ) { result = (int)CODE.ZERO; } if (result != (int)CODE.ZERO)//safe to proceed { ILanguages _DbLanguages = new DbLanguages(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { language = _DbLanguages.findLanguageByLanguageId(languageId); trScope.Complete(); } } catch (TransactionAbortedException taEx) { result = (int)CODE.ZERO; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { result = (int)CODE.ZERO; Log.Add(aEx.ToString()); } catch (Exception ex) { result = (int)CODE.ZERO; Log.Add(ex.ToString()); } } else { result = (int)CODE.ZERO; } if (result == (int)CODE.ZERO || language == null) { return(null); } else { return(language); } }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertLanguage(Language language) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate languageName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(language.languageName) || !Validate.hasMinLength(language.languageName, 1) || !Validate.hasMaxLength(language.languageName, 15) || !Validate.isAllLetters(language.languageName) ) { returnCode = (int)CODE.CTRLANGUAGE_INSERTLANGUAGE_INVALIDLANGUAGENAME; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { language.languageName = language.languageName; ILanguages _DbLanguages = new DbLanguages(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbLanguages.insertLanguage(language); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRLANGUAGE_INSERTLANGUAGE_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRLANGUAGE_INSERTLANGUAGE_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRLANGUAGE_INSERTLANGUAGE_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertText(Text text) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate textData if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(text.textData) || !Validate.hasMinLength(text.textData, 1) || !Validate.hasMaxLength(text.textData, 40000) ) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_INVALID_TEXTDATA; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { text.textData = text.textData; ITexts _DbTexts = new DbTexts(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbTexts.insertText(text); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertFile(File file) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate -> ToDo if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO ) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_INVALID_TEXTDATA; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { IFiles _DbFiles = new DbFiles(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbFiles.insertFile(file); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRFILE_INSERTFILE_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRFILE_INSERTFILE_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRFILE_INSERTFILE_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not private int insertJob(Job job, bool isText, bool isFile) { if ((isText && isFile) || (!isText && !isFile)) { return((int)CODE.CTRJOB_INSERTJOB_ISBOTHORNEITHER); //means the functions was called wrong } int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate jobName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(job.jobName) || !Validate.isAlphaNumericWithUnderscoreAndSpaceAndDash(job.jobName) || !Validate.hasMaxLength(job.jobName, 50) || !Validate.hasMinLength(job.jobName, 1) ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_JOBNAME; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_JOBNAME; } result = (int)CODE.ZERO; } //validate dateCreated if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO //nothing to validate here at this point ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_DATECREATED; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_DATECREATED; } result = (int)CODE.ZERO; } //validate durationInDays if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.durationInDays.Equals(null) || !Validate.integerIsBiggerThan(job.durationInDays, 0) || !Validate.integerIsSmallerThan(job.durationInDays, 32) ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_DURATIONINDAYS; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_DURATIONINDAYS; } result = (int)CODE.ZERO; } //validate reward if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.reward.Equals(null) || !Validate.decimalIsBiggerThan(job.reward, 0) ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_REWARD; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_REWARD; } result = (int)CODE.ZERO; } //validate dateAwarded if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO //nothing to validate here at this point ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_DATEAWARDED; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_DATEAWARDED; } result = (int)CODE.ZERO; } //validate languageFrom //has to exist CtrLanguage _CtrLanguageFrom = new CtrLanguage(); if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.languageFrom.Equals(null) || _CtrLanguageFrom.findLanguageByLanguageId(job.languageFrom.languageId) == null ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_LANGUAGEFROM; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_LANGUAGEFROM; } result = (int)CODE.ZERO; } //validate languageTo //has to exist CtrLanguage _CtrLanguageTo = new CtrLanguage(); if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.languageTo.Equals(null) || _CtrLanguageTo.findLanguageByLanguageId(job.languageTo.languageId) == null ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_LANGUAGETO; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_LANGUAGETO; } result = (int)CODE.ZERO; } //validate user //has to exist CtrUser _CtrUser = new CtrUser(); if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.user.Equals(null) || _CtrUser.findUserByUserId(job.user.userId) == null ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_USER; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_USER; } result = (int)CODE.ZERO; } //validate upload //does not have to exist; will be created if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || job.upload.Equals(null) ) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_INVALID_UPLOAD; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_INVALID_UPLOAD; } result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { job.jobName = job.jobName; job.dateCreated = DateTimeOffset.Now; job.durationInDays = job.durationInDays; job.reward = job.reward; job.dateAwarded = null; job.languageFrom = job.languageFrom; job.languageTo = job.languageTo; job.user = job.user; job.upload = job.upload; CtrUpload _CtrUpload = new CtrUpload(); IJobs _DbJobs = new DbJobs(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { if (isText) { returnCode = _CtrUpload.insertUploadText(job.upload); } if (isFile) { returnCode = _CtrUpload.insertUploadFile(job.upload); } if (returnCode >= (int)CODE.TRANSLATO_DATABASE_SEED)//means upload was inserted successfully { job.upload.uploadId = returnCode; returnCode = _DbJobs.insertJob(job); } else {//means upload failed to be inserted trScope.Dispose(); } trScope.Complete(); } } catch (TransactionAbortedException taEx) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_EXCEPTION; } Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_EXCEPTION; } Log.Add(aEx.ToString()); } catch (Exception ex) { if (isText) { returnCode = (int)CODE.CTRJOB_INSERTJOBTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRJOB_INSERTJOBFILE_EXCEPTION; } Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not private int insertSubmission(Submission submission, bool isText, bool isFile) { if ((isText && isFile) || (!isText && !isFile)) { return((int)CODE.CTRSUBMISSION_INSERTSUBMISSION_ISBOTHORNEITHER); //means the functions was called wrong } int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate dateSubmitted if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO //nothing to validate here at this point ) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_INVALID_DATESUBMITTED; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_INVALID_DATESUBMITTED; } result = (int)CODE.ZERO; } //validate isAwarded if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO //nothing to validate here at this point ) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_INVALID_ISAWARDED; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_INVALID_ISAWARDED; } result = (int)CODE.ZERO; } //validate user //has to exist CtrUser _CtrUser = new CtrUser(); if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || submission.user.Equals(null) || _CtrUser.findUserByUserId(submission.user.userId) == null ) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_INVALID_USER; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_INVALID_USER; } result = (int)CODE.ZERO; } //validate upload //does not have to exist; will be created if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || submission.upload.Equals(null) ) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_INVALID_UPLOAD; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_INVALID_UPLOAD; } result = (int)CODE.ZERO; } //validate job //has to exist CtrJob _CtrJob = new CtrJob(); if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || submission.job.Equals(null) || _CtrJob.findJobByJobId(submission.job.jobId) == null ) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_INVALID_JOB; } result = (int)CODE.ZERO; if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_INVALID_JOB; } result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { submission.dateSubmitted = DateTimeOffset.Now; submission.isAwarded = false; submission.user = submission.user; submission.upload = submission.upload; submission.job = submission.job; CtrUpload _CtrUpload = new CtrUpload(); ISubmissions _DbSubmissions = new DbSubmissions(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { if (isText) { returnCode = _CtrUpload.insertUploadText(submission.upload); } if (isFile) { returnCode = _CtrUpload.insertUploadFile(submission.upload); } if (returnCode >= (int)CODE.TRANSLATO_DATABASE_SEED)//means upload was inserted successfully { submission.upload.uploadId = returnCode; returnCode = _DbSubmissions.insertSubmission(submission); } else {//means upload failed to be inserted trScope.Dispose(); } trScope.Complete(); } } catch (TransactionAbortedException taEx) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_EXCEPTION; } Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_EXCEPTION; } Log.Add(aEx.ToString()); } catch (Exception ex) { if (isText) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONTEXT_EXCEPTION; } if (isFile) { returnCode = (int)CODE.CTRSUBMISSION_INSERTSUBMISSIONFILE_EXCEPTION; } Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertUploadFile(Upload upload) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate only file if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || upload.text != null || upload.file == null ) { returnCode = (int)CODE.CTRUPLOAD_INSERTUPLOADFILE_FAILED_ONLYFILE; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { upload.text = null; upload.file = upload.file; CtrFile _CtrFile = new CtrFile(); IUploads _DbUploads = new DbUploads(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _CtrFile.insertFile(upload.file); if (returnCode >= (int)CODE.TRANSLATO_DATABASE_SEED)//means file was inserted successfully { upload.file.fileId = returnCode; returnCode = _DbUploads.insertUploadFile(upload); } else {//means file failed to be inserted trScope.Dispose(); } trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRUPLOAD_INSERTUPLOADFILE_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRUPLOAD_INSERTUPLOADFILE_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRUPLOAD_INSERTUPLOADFILE_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertUser(User user) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate userName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.userName) || !Validate.isAlphaNumericWithUnderscore(user.userName) || !Validate.hasMinLength(user.userName, 5) || !Validate.hasMaxLength(user.userName, 15) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_USERNAME; result = (int)CODE.ZERO; } //validate password(stored in the hashedPassword field at this point. Will be replaced with hash + salt later) if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.hashedPassword) || !Validate.hasMinLength(user.hashedPassword, 8) || !Validate.hasMaxLength(user.hashedPassword, 100) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_PASSWORD; result = (int)CODE.ZERO; } //validate firstName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.firstName) || !Validate.hasMinLength(user.firstName, 2) || !Validate.hasMaxLength(user.firstName, 20) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_FIRSTNAME; result = (int)CODE.ZERO; } //validate lastName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.lastName) || !Validate.hasMinLength(user.lastName, 2) || !Validate.hasMaxLength(user.lastName, 20) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_LASTNAME; result = (int)CODE.ZERO; } //validate email if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.email) || !Validate.hasMinLength(user.email, 5) || !Validate.hasMaxLength(user.email, 50) || !user.email.Contains("@") ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_EMAIL; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { user.userName = user.userName; user.hashedPassword = Security.hashPassword(user.hashedPassword); user.firstName = user.firstName; user.lastName = user.lastName; user.email = user.email; user.newsletterOptOut = user.newsletterOptOut; user.createdOn = DateTimeOffset.Now; IUsers _DbUsers = new DbUsers(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbUsers.insertUser(user); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }