private TVTypeUserAuthorization GetFilledRandomTVTypeUserAuthorization(string OmitPropName) { TVTypeUserAuthorization tvTypeUserAuthorization = new TVTypeUserAuthorization(); if (OmitPropName != "ContactTVItemID") { tvTypeUserAuthorization.ContactTVItemID = 2; } if (OmitPropName != "TVType") { tvTypeUserAuthorization.TVType = (TVTypeEnum)GetRandomEnumType(typeof(TVTypeEnum)); } if (OmitPropName != "TVAuth") { tvTypeUserAuthorization.TVAuth = (TVAuthEnum)GetRandomEnumType(typeof(TVAuthEnum)); } if (OmitPropName != "LastUpdateDate_UTC") { tvTypeUserAuthorization.LastUpdateDate_UTC = new DateTime(2005, 3, 6); } if (OmitPropName != "LastUpdateContactTVItemID") { tvTypeUserAuthorization.LastUpdateContactTVItemID = 2; } return(tvTypeUserAuthorization); }
public TVTypeUserAuthorization GetTVTypeUserAuthorizationWithTVTypeUserAuthorizationIDDB(int TVTypeUserAuthorizationID) { TVTypeUserAuthorization TVTypeUserAuthorization = (from c in db.TVTypeUserAuthorizations where c.TVTypeUserAuthorizationID == TVTypeUserAuthorizationID select c).FirstOrDefault <TVTypeUserAuthorization>(); return(TVTypeUserAuthorization); }
public TVTypeUserAuthorization GetTVTypeUserAuthorizationWithContactTVItemIDAndTVTypeDB(int ContactTVItemID, TVTypeEnum TVType) { TVTypeUserAuthorization TVTypeUserAuthorization = (from c in db.TVTypeUserAuthorizations where c.ContactTVItemID == ContactTVItemID && c.TVType == (int)TVType select c).FirstOrDefault <TVTypeUserAuthorization>(); return(TVTypeUserAuthorization); }
public void TVTypeUserAuthorization_Controller_Put_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { TVTypeUserAuthorizationController tvTypeUserAuthorizationController = new TVTypeUserAuthorizationController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(tvTypeUserAuthorizationController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, tvTypeUserAuthorizationController.DatabaseType); TVTypeUserAuthorization tvTypeUserAuthorizationLast = new TVTypeUserAuthorization(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(query, db, ContactID); tvTypeUserAuthorizationLast = (from c in db.TVTypeUserAuthorizations select c).FirstOrDefault(); } // ok with TVTypeUserAuthorization info IHttpActionResult jsonRet = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationWithID(tvTypeUserAuthorizationLast.TVTypeUserAuthorizationID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <TVTypeUserAuthorization> Ret = jsonRet as OkNegotiatedContentResult <TVTypeUserAuthorization>; TVTypeUserAuthorization tvTypeUserAuthorizationRet = Ret.Content; Assert.AreEqual(tvTypeUserAuthorizationLast.TVTypeUserAuthorizationID, tvTypeUserAuthorizationRet.TVTypeUserAuthorizationID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Put to return success IHttpActionResult jsonRet2 = tvTypeUserAuthorizationController.Put(tvTypeUserAuthorizationRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet2 = jsonRet2 as OkNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNotNull(tvTypeUserAuthorizationRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNull(badRequest2); // Put to return CSSPError because TVTypeUserAuthorizationID of 0 does not exist tvTypeUserAuthorizationRet.TVTypeUserAuthorizationID = 0; IHttpActionResult jsonRet3 = tvTypeUserAuthorizationController.Put(tvTypeUserAuthorizationRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); OkNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet3 = jsonRet3 as OkNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNull(tvTypeUserAuthorizationRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest3); } } }
public void TVTypeUserAuthorization_CRUD_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } TVTypeUserAuthorization tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); // ------------------------------- // ------------------------------- // CRUD testing // ------------------------------- // ------------------------------- count = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().Count(); Assert.AreEqual(count, (from c in dbTestDB.TVTypeUserAuthorizations select c).Count()); tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); if (tvTypeUserAuthorization.HasErrors) { Assert.AreEqual("", tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(true, tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().Where(c => c == tvTypeUserAuthorization).Any()); tvTypeUserAuthorizationService.Update(tvTypeUserAuthorization); if (tvTypeUserAuthorization.HasErrors) { Assert.AreEqual("", tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count + 1, tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().Count()); tvTypeUserAuthorizationService.Delete(tvTypeUserAuthorization); if (tvTypeUserAuthorization.HasErrors) { Assert.AreEqual("", tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count, tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().Count()); } } }
public IHttpActionResult GetTVTypeUserAuthorizationWithID([FromUri] int TVTypeUserAuthorizationID, [FromUri] string lang = "en", [FromUri] string extra = "") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); tvTypeUserAuthorizationService.Query = tvTypeUserAuthorizationService.FillQuery(typeof(TVTypeUserAuthorization), lang, 0, 1, "", "", extra); if (tvTypeUserAuthorizationService.Query.Extra == "A") { TVTypeUserAuthorizationExtraA tvTypeUserAuthorizationExtraA = new TVTypeUserAuthorizationExtraA(); tvTypeUserAuthorizationExtraA = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationExtraAWithTVTypeUserAuthorizationID(TVTypeUserAuthorizationID); if (tvTypeUserAuthorizationExtraA == null) { return(NotFound()); } return(Ok(tvTypeUserAuthorizationExtraA)); } else if (tvTypeUserAuthorizationService.Query.Extra == "B") { TVTypeUserAuthorizationExtraB tvTypeUserAuthorizationExtraB = new TVTypeUserAuthorizationExtraB(); tvTypeUserAuthorizationExtraB = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationExtraBWithTVTypeUserAuthorizationID(TVTypeUserAuthorizationID); if (tvTypeUserAuthorizationExtraB == null) { return(NotFound()); } return(Ok(tvTypeUserAuthorizationExtraB)); } else { TVTypeUserAuthorization tvTypeUserAuthorization = new TVTypeUserAuthorization(); tvTypeUserAuthorization = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationWithTVTypeUserAuthorizationID(TVTypeUserAuthorizationID); if (tvTypeUserAuthorization == null) { return(NotFound()); } return(Ok(tvTypeUserAuthorization)); } } }
public TVTypeUserAuthorizationModel PostSetTVTypeUserAuthorizationDB(TVTypeUserAuthorizationModel tvTypeUserAuthorizationModel) { TVTypeUserAuthorization tvTypeUserAuthorizationToUpdate = GetTVTypeUserAuthorizationWithContactTVItemIDAndTVTypeDB(tvTypeUserAuthorizationModel.ContactTVItemID, tvTypeUserAuthorizationModel.TVType); if (tvTypeUserAuthorizationToUpdate == null) { tvTypeUserAuthorizationModel.DBCommand = DBCommandEnum.Original; return(PostAddTVTypeUserAuthorizationDB(tvTypeUserAuthorizationModel)); } else { return(PostUpdateTVTypeUserAuthorizationDB(tvTypeUserAuthorizationModel)); } }
/// <summary> /// Updates an [TVTypeUserAuthorization](CSSPModels.TVTypeUserAuthorization.html) item in CSSPDB /// </summary> /// <param name="tvTypeUserAuthorization">Is the TVTypeUserAuthorization item the client want to add to CSSPDB. What's important here is the TVTypeUserAuthorizationID</param> /// <returns>true if TVTypeUserAuthorization item was updated to CSSPDB, false if an error happened during the DB requested transtaction</returns> public bool Update(TVTypeUserAuthorization tvTypeUserAuthorization) { tvTypeUserAuthorization.ValidationResults = Validate(new ValidationContext(tvTypeUserAuthorization), ActionDBTypeEnum.Update); if (tvTypeUserAuthorization.ValidationResults.Count() > 0) { return(false); } db.TVTypeUserAuthorizations.Update(tvTypeUserAuthorization); if (!TryToSave(tvTypeUserAuthorization)) { return(false); } return(true); }
/// <summary> /// Tries to execute the CSSPDB transaction (add/delete/update) on an [TVTypeUserAuthorization](CSSPModels.TVTypeUserAuthorization.html) item /// </summary> /// <param name="tvTypeUserAuthorization">Is the TVTypeUserAuthorization item the client want to add to CSSPDB. What's important here is the TVTypeUserAuthorizationID</param> /// <returns>true if TVTypeUserAuthorization item was updated to CSSPDB, false if an error happened during the DB requested transtaction</returns> private bool TryToSave(TVTypeUserAuthorization tvTypeUserAuthorization) { try { db.SaveChanges(); } catch (DbUpdateException ex) { tvTypeUserAuthorization.ValidationResults = new List <ValidationResult>() { new ValidationResult(ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")) }.AsEnumerable(); return(false); } return(true); }
// Fill public string FillTVTypeUserAuthorization(TVTypeUserAuthorization tvTypeUserAuthorization, TVTypeUserAuthorizationModel tvTypeUserAuthorizationModel, ContactOK contactOK) { tvTypeUserAuthorization.DBCommand = (int)tvTypeUserAuthorizationModel.DBCommand; tvTypeUserAuthorization.ContactTVItemID = tvTypeUserAuthorizationModel.ContactTVItemID; tvTypeUserAuthorization.TVType = (int)tvTypeUserAuthorizationModel.TVType; tvTypeUserAuthorization.TVAuth = (int)tvTypeUserAuthorizationModel.TVAuth; tvTypeUserAuthorization.LastUpdateDate_UTC = DateTime.UtcNow; if (contactOK == null) { tvTypeUserAuthorization.LastUpdateContactTVItemID = 2; } else { tvTypeUserAuthorization.LastUpdateContactTVItemID = contactOK.ContactTVItemID; } return(""); }
public void TVTypeUserAuthorization_Controller_GetTVTypeUserAuthorizationWithID_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { TVTypeUserAuthorizationController tvTypeUserAuthorizationController = new TVTypeUserAuthorizationController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(tvTypeUserAuthorizationController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, tvTypeUserAuthorizationController.DatabaseType); TVTypeUserAuthorization tvTypeUserAuthorizationFirst = new TVTypeUserAuthorization(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query(), db, ContactID); tvTypeUserAuthorizationFirst = (from c in db.TVTypeUserAuthorizations select c).FirstOrDefault(); } // ok with TVTypeUserAuthorization info IHttpActionResult jsonRet = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationWithID(tvTypeUserAuthorizationFirst.TVTypeUserAuthorizationID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <TVTypeUserAuthorization> Ret = jsonRet as OkNegotiatedContentResult <TVTypeUserAuthorization>; TVTypeUserAuthorization tvTypeUserAuthorizationRet = Ret.Content; Assert.AreEqual(tvTypeUserAuthorizationFirst.TVTypeUserAuthorizationID, tvTypeUserAuthorizationRet.TVTypeUserAuthorizationID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Not Found IHttpActionResult jsonRet2 = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationWithID(0); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet2 = jsonRet2 as OkNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNull(tvTypeUserAuthorizationRet2); NotFoundResult notFoundRequest = jsonRet2 as NotFoundResult; Assert.IsNotNull(notFoundRequest); } } }
public IHttpActionResult Delete([FromBody] TVTypeUserAuthorization tvTypeUserAuthorization, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!tvTypeUserAuthorizationService.Delete(tvTypeUserAuthorization)) { return(BadRequest(String.Join("|||", tvTypeUserAuthorization.ValidationResults))); } else { tvTypeUserAuthorization.ValidationResults = null; return(Ok(tvTypeUserAuthorization)); } } }
public void GetTVTypeUserAuthorizationList_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); TVTypeUserAuthorization tvTypeUserAuthorization = (from c in dbTestDB.TVTypeUserAuthorizations select c).FirstOrDefault(); Assert.IsNotNull(tvTypeUserAuthorization); List <TVTypeUserAuthorization> tvTypeUserAuthorizationDirectQueryList = new List <TVTypeUserAuthorization>(); tvTypeUserAuthorizationDirectQueryList = (from c in dbTestDB.TVTypeUserAuthorizations select c).Take(200).ToList(); foreach (string extra in new List <string>() { null, "A", "B", "C", "D", "E" }) { tvTypeUserAuthorizationService.Query.Extra = extra; if (string.IsNullOrWhiteSpace(extra)) { List <TVTypeUserAuthorization> tvTypeUserAuthorizationList = new List <TVTypeUserAuthorization>(); tvTypeUserAuthorizationList = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().ToList(); CheckTVTypeUserAuthorizationFields(tvTypeUserAuthorizationList); } else { //Assert.AreEqual(true, false); } } } } }
public TVTypeUserAuthorizationTest() { tVTypeUserAuthorization = new TVTypeUserAuthorization(); tVTypeUserAuthorizationExtraA = new TVTypeUserAuthorizationExtraA(); tVTypeUserAuthorizationExtraB = new TVTypeUserAuthorizationExtraB(); }
public TVTypeUserAuthorizationModel PostUpdateTVTypeUserAuthorizationDB(TVTypeUserAuthorizationModel tvTypeUserAuthorizationModel) { string retStr = TVTypeUserAuthorizationModelOK(tvTypeUserAuthorizationModel); if (!string.IsNullOrEmpty(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } bool IsAdmin = IsAdministratorDB(User.Identity.Name); if (!IsAdmin) { return(ReturnError(ServiceRes.OnlyAdministratorsCanManageUsers)); } ContactModel contactModel = GetContactModelWithContactTVItemIDDB(tvTypeUserAuthorizationModel.ContactTVItemID); if (!string.IsNullOrWhiteSpace(contactModel.Error)) { return(ReturnError(contactModel.Error)); } ContactModel contactModelLoggedIn = GetContactLoggedInDB(); if (!string.IsNullOrWhiteSpace(contactModelLoggedIn.Error)) { return(ReturnError(contactModelLoggedIn.Error)); } if (contactOK.ContactTVItemID == tvTypeUserAuthorizationModel.ContactTVItemID) { return(ReturnError(ServiceRes.CantSetOwnAuthorization)); } TVTypeUserAuthorization tvTypeUserAuthorizationToUpdate = new TVTypeUserAuthorization(); if (tvTypeUserAuthorizationModel.TVTypeUserAuthorizationID != 0) { tvTypeUserAuthorizationToUpdate = GetTVTypeUserAuthorizationWithTVTypeUserAuthorizationIDDB(tvTypeUserAuthorizationModel.TVTypeUserAuthorizationID); if (tvTypeUserAuthorizationToUpdate == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToUpdate, BaseEnumServiceRes.TVTypeUserAuthorization))); } } else { tvTypeUserAuthorizationToUpdate = GetTVTypeUserAuthorizationWithContactTVItemIDAndTVTypeDB(tvTypeUserAuthorizationModel.ContactTVItemID, tvTypeUserAuthorizationModel.TVType); if (tvTypeUserAuthorizationToUpdate == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToUpdate, BaseEnumServiceRes.TVTypeUserAuthorization))); } } retStr = FillTVTypeUserAuthorization(tvTypeUserAuthorizationToUpdate, tvTypeUserAuthorizationModel, contactOK); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } using (TransactionScope ts = new TransactionScope()) { retStr = DoUpdateChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("TVTypeUserAuthorizations", tvTypeUserAuthorizationToUpdate.TVTypeUserAuthorizationID, LogCommandEnum.Change, tvTypeUserAuthorizationToUpdate); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } if (tvTypeUserAuthorizationModel.TVType == TVTypeEnum.Root) { if (tvTypeUserAuthorizationModel.TVAuth == TVAuthEnum.Admin) { contactModel.IsAdmin = true; } else { contactModel.IsAdmin = false; } ContactService contactService = new ContactService(LanguageRequest, User); ContactModel contactModelRet = contactService.PostUpdateContactDB(contactModel); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } } return(GetTVTypeUserAuthorizationModelWithTVTypeUserAuthorizationIDDB(tvTypeUserAuthorizationToUpdate.TVTypeUserAuthorizationID)); }
public TVTypeUserAuthorizationModel PostDeleteTVTypeUserAuthorizationDB(int TVTypeUserAuthorizationID) { ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } bool IsAdmin = IsAdministratorDB(User.Identity.Name); if (!IsAdmin) { return(ReturnError(ServiceRes.OnlyAdministratorsCanManageUsers)); } TVTypeUserAuthorization tvTypeUserAuthorizationToDelete = GetTVTypeUserAuthorizationWithTVTypeUserAuthorizationIDDB(TVTypeUserAuthorizationID); if (tvTypeUserAuthorizationToDelete == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToDelete, BaseEnumServiceRes.TVTypeUserAuthorization))); } if (tvTypeUserAuthorizationToDelete.TVType == (int)TVTypeEnum.Root) { return(ReturnError(ServiceRes.CantRemoveRootAutorization)); } ContactModel contactModel = GetContactModelWithContactTVItemIDDB(tvTypeUserAuthorizationToDelete.ContactTVItemID); if (!string.IsNullOrWhiteSpace(contactModel.Error)) { return(ReturnError(contactModel.Error)); } ContactModel contactModelLoggedIn = GetContactLoggedInDB(); if (!string.IsNullOrWhiteSpace(contactModelLoggedIn.Error)) { return(ReturnError(contactModelLoggedIn.Error)); } if (contactOK.ContactTVItemID == tvTypeUserAuthorizationToDelete.ContactTVItemID) { return(ReturnError(ServiceRes.CantSetOwnAuthorization)); } using (TransactionScope ts = new TransactionScope()) { db.TVTypeUserAuthorizations.Remove(tvTypeUserAuthorizationToDelete); string retStr = DoDeleteChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("TVTypeUserAuthorizations", tvTypeUserAuthorizationToDelete.TVTypeUserAuthorizationID, LogCommandEnum.Delete, tvTypeUserAuthorizationToDelete); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(ReturnError("")); }
// Post public TVTypeUserAuthorizationModel PostAddTVTypeUserAuthorizationDB(TVTypeUserAuthorizationModel tvTypeUserAuthorizationModel) { string retStr = TVTypeUserAuthorizationModelOK(tvTypeUserAuthorizationModel); if (!string.IsNullOrEmpty(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } bool IsAdmin = IsAdministratorDB(User.Identity.Name); if (!IsAdmin) { return(ReturnError(ServiceRes.OnlyAdministratorsCanManageUsers)); } ContactModel contactModel = GetContactModelWithContactTVItemIDDB(tvTypeUserAuthorizationModel.ContactTVItemID); if (!string.IsNullOrWhiteSpace(contactModel.Error)) { return(ReturnError(contactModel.Error)); } ContactModel contactModelLoggedIn = GetContactLoggedInDB(); if (!string.IsNullOrWhiteSpace(contactModelLoggedIn.Error)) { return(ReturnError(contactModelLoggedIn.Error)); } if (contactOK.ContactTVItemID == tvTypeUserAuthorizationModel.ContactTVItemID) { return(ReturnError(ServiceRes.CantSetOwnAuthorization)); } TVTypeUserAuthorization tvTypeUserAuthorizationExist = GetTVTypeUserAuthorizationWithContactTVItemIDAndTVTypeDB(tvTypeUserAuthorizationModel.ContactTVItemID, tvTypeUserAuthorizationModel.TVType); if (tvTypeUserAuthorizationExist != null) { return(ReturnError(string.Format(ServiceRes._AlreadyExists, BaseEnumServiceRes.TVTypeUserAuthorization))); } TVTypeUserAuthorization tvTypeUserAuthorizationNew = new TVTypeUserAuthorization(); retStr = FillTVTypeUserAuthorization(tvTypeUserAuthorizationNew, tvTypeUserAuthorizationModel, contactOK); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } using (TransactionScope ts = new TransactionScope()) { db.TVTypeUserAuthorizations.Add(tvTypeUserAuthorizationNew); retStr = DoAddChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("TVTypeUserAuthorizations", tvTypeUserAuthorizationNew.TVTypeUserAuthorizationID, LogCommandEnum.Add, tvTypeUserAuthorizationNew); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(GetTVTypeUserAuthorizationModelWithTVTypeUserAuthorizationIDDB(tvTypeUserAuthorizationNew.TVTypeUserAuthorizationID)); }
public void TVTypeUserAuthorization_Controller_GetTVTypeUserAuthorizationList_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { TVTypeUserAuthorizationController tvTypeUserAuthorizationController = new TVTypeUserAuthorizationController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(tvTypeUserAuthorizationController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, tvTypeUserAuthorizationController.DatabaseType); TVTypeUserAuthorization tvTypeUserAuthorizationFirst = new TVTypeUserAuthorization(); int count = -1; Query query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(query, db, ContactID); tvTypeUserAuthorizationFirst = (from c in db.TVTypeUserAuthorizations select c).FirstOrDefault(); count = (from c in db.TVTypeUserAuthorizations select c).Count(); count = (query.Take > count ? count : query.Take); } // ok with TVTypeUserAuthorization info IHttpActionResult jsonRet = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationList(); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <List <TVTypeUserAuthorization> > ret = jsonRet as OkNegotiatedContentResult <List <TVTypeUserAuthorization> >; Assert.AreEqual(tvTypeUserAuthorizationFirst.TVTypeUserAuthorizationID, ret.Content[0].TVTypeUserAuthorizationID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); List <TVTypeUserAuthorization> tvTypeUserAuthorizationList = new List <TVTypeUserAuthorization>(); count = -1; query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(query, db, ContactID); tvTypeUserAuthorizationList = (from c in db.TVTypeUserAuthorizations select c).OrderBy(c => c.TVTypeUserAuthorizationID).Skip(0).Take(2).ToList(); count = (from c in db.TVTypeUserAuthorizations select c).Count(); } if (count > 0) { query.Skip = 0; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with TVTypeUserAuthorization info jsonRet = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet); ret = jsonRet as OkNegotiatedContentResult <List <TVTypeUserAuthorization> >; Assert.AreEqual(tvTypeUserAuthorizationList[0].TVTypeUserAuthorizationID, ret.Content[0].TVTypeUserAuthorizationID); 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 TVTypeUserAuthorization info IHttpActionResult jsonRet2 = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <List <TVTypeUserAuthorization> > ret2 = jsonRet2 as OkNegotiatedContentResult <List <TVTypeUserAuthorization> >; Assert.AreEqual(tvTypeUserAuthorizationList[1].TVTypeUserAuthorizationID, ret2.Content[0].TVTypeUserAuthorizationID); Assert.AreEqual((count > query.Take ? query.Take : count), ret2.Content.Count); } } } } }
public void TVTypeUserAuthorization_Controller_Post_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { TVTypeUserAuthorizationController tvTypeUserAuthorizationController = new TVTypeUserAuthorizationController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(tvTypeUserAuthorizationController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, tvTypeUserAuthorizationController.DatabaseType); TVTypeUserAuthorization tvTypeUserAuthorizationLast = new TVTypeUserAuthorization(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; query.Asc = ""; query.Desc = ""; TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(query, db, ContactID); tvTypeUserAuthorizationLast = (from c in db.TVTypeUserAuthorizations select c).FirstOrDefault(); } // ok with TVTypeUserAuthorization info IHttpActionResult jsonRet = tvTypeUserAuthorizationController.GetTVTypeUserAuthorizationWithID(tvTypeUserAuthorizationLast.TVTypeUserAuthorizationID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <TVTypeUserAuthorization> Ret = jsonRet as OkNegotiatedContentResult <TVTypeUserAuthorization>; TVTypeUserAuthorization tvTypeUserAuthorizationRet = Ret.Content; Assert.AreEqual(tvTypeUserAuthorizationLast.TVTypeUserAuthorizationID, tvTypeUserAuthorizationRet.TVTypeUserAuthorizationID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Post to return CSSPError because TVTypeUserAuthorizationID exist IHttpActionResult jsonRet2 = tvTypeUserAuthorizationController.Post(tvTypeUserAuthorizationRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet2 = jsonRet2 as OkNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNull(tvTypeUserAuthorizationRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest2); // Post to return newly added TVTypeUserAuthorization tvTypeUserAuthorizationRet.TVTypeUserAuthorizationID = 0; tvTypeUserAuthorizationController.Request = new System.Net.Http.HttpRequestMessage(); tvTypeUserAuthorizationController.Request.RequestUri = new System.Uri("http://localhost:5000/api/tvTypeUserAuthorization"); IHttpActionResult jsonRet3 = tvTypeUserAuthorizationController.Post(tvTypeUserAuthorizationRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); CreatedNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet3 = jsonRet3 as CreatedNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNotNull(tvTypeUserAuthorizationRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNull(badRequest3); IHttpActionResult jsonRet4 = tvTypeUserAuthorizationController.Delete(tvTypeUserAuthorizationRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet4); OkNegotiatedContentResult <TVTypeUserAuthorization> tvTypeUserAuthorizationRet4 = jsonRet4 as OkNegotiatedContentResult <TVTypeUserAuthorization>; Assert.IsNotNull(tvTypeUserAuthorizationRet4); BadRequestErrorMessageResult badRequest4 = jsonRet4 as BadRequestErrorMessageResult; Assert.IsNull(badRequest4); } } }
/// <summary> /// Validate function for all TVTypeUserAuthorizationService commands /// </summary> /// <param name="validationContext">System.ComponentModel.DataAnnotations.ValidationContext (Describes the context in which a validation check is performed.)</param> /// <param name="actionDBType">[ActionDBTypeEnum] (CSSPEnums.ActionDBTypeEnum.html) action type to validate</param> /// <returns>IEnumerable of ValidationResult (Where ValidationResult is a container for the results of a validation request.)</returns> private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType) { string retStr = ""; Enums enums = new Enums(LanguageRequest); TVTypeUserAuthorization tvTypeUserAuthorization = validationContext.ObjectInstance as TVTypeUserAuthorization; tvTypeUserAuthorization.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (tvTypeUserAuthorization.TVTypeUserAuthorizationID == 0) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "TVTypeUserAuthorizationID"), new[] { "TVTypeUserAuthorizationID" })); } if (!(from c in db.TVTypeUserAuthorizations select c).Where(c => c.TVTypeUserAuthorizationID == tvTypeUserAuthorization.TVTypeUserAuthorizationID).Any()) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVTypeUserAuthorization", "TVTypeUserAuthorizationID", tvTypeUserAuthorization.TVTypeUserAuthorizationID.ToString()), new[] { "TVTypeUserAuthorizationID" })); } } TVItem TVItemContactTVItemID = (from c in db.TVItems where c.TVItemID == tvTypeUserAuthorization.ContactTVItemID select c).FirstOrDefault(); if (TVItemContactTVItemID == null) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "ContactTVItemID", tvTypeUserAuthorization.ContactTVItemID.ToString()), new[] { "ContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemContactTVItemID.TVType)) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsNotOfType_, "ContactTVItemID", "Contact"), new[] { "ContactTVItemID" })); } } retStr = enums.EnumTypeOK(typeof(TVTypeEnum), (int?)tvTypeUserAuthorization.TVType); if (!string.IsNullOrWhiteSpace(retStr)) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "TVType"), new[] { "TVType" })); } retStr = enums.EnumTypeOK(typeof(TVAuthEnum), (int?)tvTypeUserAuthorization.TVAuth); if (!string.IsNullOrWhiteSpace(retStr)) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "TVAuth"), new[] { "TVAuth" })); } if (tvTypeUserAuthorization.LastUpdateDate_UTC.Year == 1) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (tvTypeUserAuthorization.LastUpdateDate_UTC.Year < 1980) { tvTypeUserAuthorization.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 == tvTypeUserAuthorization.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", tvTypeUserAuthorization.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { tvTypeUserAuthorization.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 { tvTypeUserAuthorization.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
public void TVTypeUserAuthorization_Properties_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVTypeUserAuthorizationService tvTypeUserAuthorizationService = new TVTypeUserAuthorizationService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } count = tvTypeUserAuthorizationService.GetTVTypeUserAuthorizationList().Count(); TVTypeUserAuthorization tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); // ------------------------------- // ------------------------------- // Properties testing // ------------------------------- // ------------------------------- // ----------------------------------- // [Key] // Is NOT Nullable // tvTypeUserAuthorization.TVTypeUserAuthorizationID (Int32) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.TVTypeUserAuthorizationID = 0; tvTypeUserAuthorizationService.Update(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TVTypeUserAuthorizationID"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.TVTypeUserAuthorizationID = 10000000; tvTypeUserAuthorizationService.Update(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVTypeUserAuthorization", "TVTypeUserAuthorizationID", tvTypeUserAuthorization.TVTypeUserAuthorizationID.ToString()), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // tvTypeUserAuthorization.ContactTVItemID (Int32) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.ContactTVItemID = 0; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "ContactTVItemID", tvTypeUserAuthorization.ContactTVItemID.ToString()), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.ContactTVItemID = 1; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "ContactTVItemID", "Contact"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPEnumType] // tvTypeUserAuthorization.TVType (TVTypeEnum) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.TVType = (TVTypeEnum)1000000; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TVType"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPEnumType] // tvTypeUserAuthorization.TVAuth (TVAuthEnum) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.TVAuth = (TVAuthEnum)1000000; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TVAuth"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPAfter(Year = 1980)] // tvTypeUserAuthorization.LastUpdateDate_UTC (DateTime) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.LastUpdateDate_UTC = new DateTime(); tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.LastUpdateDate_UTC = new DateTime(1979, 1, 1); tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // tvTypeUserAuthorization.LastUpdateContactTVItemID (Int32) // ----------------------------------- tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.LastUpdateContactTVItemID = 0; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", tvTypeUserAuthorization.LastUpdateContactTVItemID.ToString()), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); tvTypeUserAuthorization = null; tvTypeUserAuthorization = GetFilledRandomTVTypeUserAuthorization(""); tvTypeUserAuthorization.LastUpdateContactTVItemID = 1; tvTypeUserAuthorizationService.Add(tvTypeUserAuthorization); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), tvTypeUserAuthorization.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [NotMapped] // tvTypeUserAuthorization.HasErrors (Boolean) // ----------------------------------- // No testing requied // ----------------------------------- // Is NOT Nullable // [NotMapped] // tvTypeUserAuthorization.ValidationResults (IEnumerable`1) // ----------------------------------- // No testing requied } } }