public void ReportSectionLanguage_Controller_Put_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ReportSectionLanguageController reportSectionLanguageController = new ReportSectionLanguageController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(reportSectionLanguageController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, reportSectionLanguageController.DatabaseType); ReportSectionLanguage reportSectionLanguageLast = new ReportSectionLanguage(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(query, db, ContactID); reportSectionLanguageLast = (from c in db.ReportSectionLanguages select c).FirstOrDefault(); } // ok with ReportSectionLanguage info IHttpActionResult jsonRet = reportSectionLanguageController.GetReportSectionLanguageWithID(reportSectionLanguageLast.ReportSectionLanguageID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ReportSectionLanguage> Ret = jsonRet as OkNegotiatedContentResult <ReportSectionLanguage>; ReportSectionLanguage reportSectionLanguageRet = Ret.Content; Assert.AreEqual(reportSectionLanguageLast.ReportSectionLanguageID, reportSectionLanguageRet.ReportSectionLanguageID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Put to return success IHttpActionResult jsonRet2 = reportSectionLanguageController.Put(reportSectionLanguageRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet2 = jsonRet2 as OkNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNotNull(reportSectionLanguageRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNull(badRequest2); // Put to return CSSPError because ReportSectionLanguageID of 0 does not exist reportSectionLanguageRet.ReportSectionLanguageID = 0; IHttpActionResult jsonRet3 = reportSectionLanguageController.Put(reportSectionLanguageRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); OkNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet3 = jsonRet3 as OkNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNull(reportSectionLanguageRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest3); } } }
public IHttpActionResult GetReportSectionLanguageWithID([FromUri] int ReportSectionLanguageID, [FromUri] string lang = "en", [FromUri] string extra = "") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); reportSectionLanguageService.Query = reportSectionLanguageService.FillQuery(typeof(ReportSectionLanguage), lang, 0, 1, "", "", extra); if (reportSectionLanguageService.Query.Extra == "A") { ReportSectionLanguageExtraA reportSectionLanguageExtraA = new ReportSectionLanguageExtraA(); reportSectionLanguageExtraA = reportSectionLanguageService.GetReportSectionLanguageExtraAWithReportSectionLanguageID(ReportSectionLanguageID); if (reportSectionLanguageExtraA == null) { return(NotFound()); } return(Ok(reportSectionLanguageExtraA)); } else if (reportSectionLanguageService.Query.Extra == "B") { ReportSectionLanguageExtraB reportSectionLanguageExtraB = new ReportSectionLanguageExtraB(); reportSectionLanguageExtraB = reportSectionLanguageService.GetReportSectionLanguageExtraBWithReportSectionLanguageID(ReportSectionLanguageID); if (reportSectionLanguageExtraB == null) { return(NotFound()); } return(Ok(reportSectionLanguageExtraB)); } else { ReportSectionLanguage reportSectionLanguage = new ReportSectionLanguage(); reportSectionLanguage = reportSectionLanguageService.GetReportSectionLanguageWithReportSectionLanguageID(ReportSectionLanguageID); if (reportSectionLanguage == null) { return(NotFound()); } return(Ok(reportSectionLanguage)); } } }
private bool TryToSave(ReportSectionLanguage reportSectionLanguage) { try { db.SaveChanges(); } catch (DbUpdateException ex) { reportSectionLanguage.ValidationResults = new List <ValidationResult>() { new ValidationResult(ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")) }.AsEnumerable(); return(false); } return(true); }
public bool Update(ReportSectionLanguage reportSectionLanguage) { reportSectionLanguage.ValidationResults = Validate(new ValidationContext(reportSectionLanguage), ActionDBTypeEnum.Update); if (reportSectionLanguage.ValidationResults.Count() > 0) { return(false); } db.ReportSectionLanguages.Update(reportSectionLanguage); if (!TryToSave(reportSectionLanguage)) { return(false); } return(true); }
public void ReportSectionLanguage_Controller_GetReportSectionLanguageWithID_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ReportSectionLanguageController reportSectionLanguageController = new ReportSectionLanguageController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(reportSectionLanguageController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, reportSectionLanguageController.DatabaseType); ReportSectionLanguage reportSectionLanguageFirst = new ReportSectionLanguage(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(new Query(), db, ContactID); reportSectionLanguageFirst = (from c in db.ReportSectionLanguages select c).FirstOrDefault(); } // ok with ReportSectionLanguage info IHttpActionResult jsonRet = reportSectionLanguageController.GetReportSectionLanguageWithID(reportSectionLanguageFirst.ReportSectionLanguageID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ReportSectionLanguage> Ret = jsonRet as OkNegotiatedContentResult <ReportSectionLanguage>; ReportSectionLanguage reportSectionLanguageRet = Ret.Content; Assert.AreEqual(reportSectionLanguageFirst.ReportSectionLanguageID, reportSectionLanguageRet.ReportSectionLanguageID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Not Found IHttpActionResult jsonRet2 = reportSectionLanguageController.GetReportSectionLanguageWithID(0); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet2 = jsonRet2 as OkNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNull(reportSectionLanguageRet2); NotFoundResult notFoundRequest = jsonRet2 as NotFoundResult; Assert.IsNotNull(notFoundRequest); } } }
public IHttpActionResult Delete([FromBody] ReportSectionLanguage reportSectionLanguage, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!reportSectionLanguageService.Delete(reportSectionLanguage)) { return(BadRequest(String.Join("|||", reportSectionLanguage.ValidationResults))); } else { reportSectionLanguage.ValidationResults = null; return(Ok(reportSectionLanguage)); } } }
private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType) { string retStr = ""; Enums enums = new Enums(LanguageRequest); ReportSectionLanguage reportSectionLanguage = validationContext.ObjectInstance as ReportSectionLanguage; reportSectionLanguage.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (reportSectionLanguage.ReportSectionLanguageID == 0) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ReportSectionLanguageID"), new[] { "ReportSectionLanguageID" })); } if (!(from c in db.ReportSectionLanguages select c).Where(c => c.ReportSectionLanguageID == reportSectionLanguage.ReportSectionLanguageID).Any()) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "ReportSectionLanguage", "ReportSectionLanguageID", reportSectionLanguage.ReportSectionLanguageID.ToString()), new[] { "ReportSectionLanguageID" })); } } ReportSection ReportSectionReportSectionID = (from c in db.ReportSections where c.ReportSectionID == reportSectionLanguage.ReportSectionID select c).FirstOrDefault(); if (ReportSectionReportSectionID == null) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "ReportSection", "ReportSectionID", reportSectionLanguage.ReportSectionID.ToString()), new[] { "ReportSectionID" })); } retStr = enums.EnumTypeOK(typeof(LanguageEnum), (int?)reportSectionLanguage.Language); if (!string.IsNullOrWhiteSpace(retStr)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "Language"), new[] { "Language" })); } if (string.IsNullOrWhiteSpace(reportSectionLanguage.ReportSectionName)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ReportSectionName"), new[] { "ReportSectionName" })); } if (!string.IsNullOrWhiteSpace(reportSectionLanguage.ReportSectionName) && reportSectionLanguage.ReportSectionName.Length > 100) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "ReportSectionName", "100"), new[] { "ReportSectionName" })); } retStr = enums.EnumTypeOK(typeof(TranslationStatusEnum), (int?)reportSectionLanguage.TranslationStatusReportSectionName); if (!string.IsNullOrWhiteSpace(retStr)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "TranslationStatusReportSectionName"), new[] { "TranslationStatusReportSectionName" })); } if (string.IsNullOrWhiteSpace(reportSectionLanguage.ReportSectionText)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ReportSectionText"), new[] { "ReportSectionText" })); } if (!string.IsNullOrWhiteSpace(reportSectionLanguage.ReportSectionText) && reportSectionLanguage.ReportSectionText.Length > 10000) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "ReportSectionText", "10000"), new[] { "ReportSectionText" })); } retStr = enums.EnumTypeOK(typeof(TranslationStatusEnum), (int?)reportSectionLanguage.TranslationStatusReportSectionText); if (!string.IsNullOrWhiteSpace(retStr)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "TranslationStatusReportSectionText"), new[] { "TranslationStatusReportSectionText" })); } if (reportSectionLanguage.LastUpdateDate_UTC.Year == 1) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (reportSectionLanguage.LastUpdateDate_UTC.Year < 1980) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), new[] { "LastUpdateDate_UTC" })); } } TVItem TVItemLastUpdateContactTVItemID = (from c in db.TVItems where c.TVItemID == reportSectionLanguage.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", reportSectionLanguage.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), new[] { "LastUpdateContactTVItemID" })); } } retStr = ""; // added to stop compiling CSSPError if (retStr != "") // will never be true { reportSectionLanguage.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
public ReportSectionLanguageTest() { reportSectionLanguage = new ReportSectionLanguage(); reportSectionLanguageExtraA = new ReportSectionLanguageExtraA(); reportSectionLanguageExtraB = new ReportSectionLanguageExtraB(); }
public void ReportSectionLanguage_Controller_GetReportSectionLanguageList_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ReportSectionLanguageController reportSectionLanguageController = new ReportSectionLanguageController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(reportSectionLanguageController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, reportSectionLanguageController.DatabaseType); ReportSectionLanguage reportSectionLanguageFirst = new ReportSectionLanguage(); int count = -1; Query query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(query, db, ContactID); reportSectionLanguageFirst = (from c in db.ReportSectionLanguages select c).FirstOrDefault(); count = (from c in db.ReportSectionLanguages select c).Count(); count = (query.Take > count ? count : query.Take); } // ok with ReportSectionLanguage info IHttpActionResult jsonRet = reportSectionLanguageController.GetReportSectionLanguageList(); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <List <ReportSectionLanguage> > ret = jsonRet as OkNegotiatedContentResult <List <ReportSectionLanguage> >; Assert.AreEqual(reportSectionLanguageFirst.ReportSectionLanguageID, ret.Content[0].ReportSectionLanguageID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); List <ReportSectionLanguage> reportSectionLanguageList = new List <ReportSectionLanguage>(); count = -1; query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(query, db, ContactID); reportSectionLanguageList = (from c in db.ReportSectionLanguages select c).OrderBy(c => c.ReportSectionLanguageID).Skip(0).Take(2).ToList(); count = (from c in db.ReportSectionLanguages select c).Count(); } if (count > 0) { query.Skip = 0; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with ReportSectionLanguage info jsonRet = reportSectionLanguageController.GetReportSectionLanguageList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet); ret = jsonRet as OkNegotiatedContentResult <List <ReportSectionLanguage> >; Assert.AreEqual(reportSectionLanguageList[0].ReportSectionLanguageID, ret.Content[0].ReportSectionLanguageID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); if (count > 1) { query.Skip = 1; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with ReportSectionLanguage info IHttpActionResult jsonRet2 = reportSectionLanguageController.GetReportSectionLanguageList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <List <ReportSectionLanguage> > ret2 = jsonRet2 as OkNegotiatedContentResult <List <ReportSectionLanguage> >; Assert.AreEqual(reportSectionLanguageList[1].ReportSectionLanguageID, ret2.Content[0].ReportSectionLanguageID); Assert.AreEqual((count > query.Take ? query.Take : count), ret2.Content.Count); } } } } }
public void ReportSectionLanguage_Controller_Post_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ReportSectionLanguageController reportSectionLanguageController = new ReportSectionLanguageController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(reportSectionLanguageController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, reportSectionLanguageController.DatabaseType); ReportSectionLanguage reportSectionLanguageLast = new ReportSectionLanguage(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; query.Asc = ""; query.Desc = ""; ReportSectionLanguageService reportSectionLanguageService = new ReportSectionLanguageService(query, db, ContactID); reportSectionLanguageLast = (from c in db.ReportSectionLanguages select c).FirstOrDefault(); } // ok with ReportSectionLanguage info IHttpActionResult jsonRet = reportSectionLanguageController.GetReportSectionLanguageWithID(reportSectionLanguageLast.ReportSectionLanguageID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ReportSectionLanguage> Ret = jsonRet as OkNegotiatedContentResult <ReportSectionLanguage>; ReportSectionLanguage reportSectionLanguageRet = Ret.Content; Assert.AreEqual(reportSectionLanguageLast.ReportSectionLanguageID, reportSectionLanguageRet.ReportSectionLanguageID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Post to return CSSPError because ReportSectionLanguageID exist IHttpActionResult jsonRet2 = reportSectionLanguageController.Post(reportSectionLanguageRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet2 = jsonRet2 as OkNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNull(reportSectionLanguageRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest2); // Post to return newly added ReportSectionLanguage reportSectionLanguageRet.ReportSectionLanguageID = 0; reportSectionLanguageController.Request = new System.Net.Http.HttpRequestMessage(); reportSectionLanguageController.Request.RequestUri = new System.Uri("http://localhost:5000/api/reportSectionLanguage"); IHttpActionResult jsonRet3 = reportSectionLanguageController.Post(reportSectionLanguageRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); CreatedNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet3 = jsonRet3 as CreatedNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNotNull(reportSectionLanguageRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNull(badRequest3); IHttpActionResult jsonRet4 = reportSectionLanguageController.Delete(reportSectionLanguageRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet4); OkNegotiatedContentResult <ReportSectionLanguage> reportSectionLanguageRet4 = jsonRet4 as OkNegotiatedContentResult <ReportSectionLanguage>; Assert.IsNotNull(reportSectionLanguageRet4); BadRequestErrorMessageResult badRequest4 = jsonRet4 as BadRequestErrorMessageResult; Assert.IsNull(badRequest4); } } }