public void HydrometricSite_Controller_Put_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { HydrometricSiteController hydrometricSiteController = new HydrometricSiteController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(hydrometricSiteController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, hydrometricSiteController.DatabaseType); HydrometricSite hydrometricSiteLast = new HydrometricSite(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(query, db, ContactID); hydrometricSiteLast = (from c in db.HydrometricSites select c).FirstOrDefault(); } // ok with HydrometricSite info IHttpActionResult jsonRet = hydrometricSiteController.GetHydrometricSiteWithID(hydrometricSiteLast.HydrometricSiteID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <HydrometricSite> Ret = jsonRet as OkNegotiatedContentResult <HydrometricSite>; HydrometricSite hydrometricSiteRet = Ret.Content; Assert.AreEqual(hydrometricSiteLast.HydrometricSiteID, hydrometricSiteRet.HydrometricSiteID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Put to return success IHttpActionResult jsonRet2 = hydrometricSiteController.Put(hydrometricSiteRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <HydrometricSite> hydrometricSiteRet2 = jsonRet2 as OkNegotiatedContentResult <HydrometricSite>; Assert.IsNotNull(hydrometricSiteRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNull(badRequest2); // Put to return CSSPError because HydrometricSiteID of 0 does not exist hydrometricSiteRet.HydrometricSiteID = 0; IHttpActionResult jsonRet3 = hydrometricSiteController.Put(hydrometricSiteRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); OkNegotiatedContentResult <HydrometricSite> hydrometricSiteRet3 = jsonRet3 as OkNegotiatedContentResult <HydrometricSite>; Assert.IsNull(hydrometricSiteRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest3); } } }
public HydrometricSite GetHydrometricSiteWithHydrometricSiteIDDB(int HydrometricSiteID) { HydrometricSite HydrometricSite = (from c in db.HydrometricSites where c.HydrometricSiteID == HydrometricSiteID orderby c.HydrometricSiteID descending select c).FirstOrDefault <HydrometricSite>(); return(HydrometricSite); }
// Post public HydrometricSiteModel PostAddHydrometricSiteDB(HydrometricSiteModel hydrometricSiteModel) { string retStr = HydrometricSiteModelOK(hydrometricSiteModel); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } TVItemModel tvItemModelExist = _TVItemService.GetTVItemModelWithTVItemIDDB(hydrometricSiteModel.HydrometricSiteTVItemID); if (!string.IsNullOrWhiteSpace(tvItemModelExist.Error)) { return(ReturnError(tvItemModelExist.Error)); } HydrometricSite hydrometricSiteNew = new HydrometricSite(); retStr = FillHydrometricSite(hydrometricSiteNew, hydrometricSiteModel, contactOK); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } using (TransactionScope ts = new TransactionScope()) { db.HydrometricSites.Add(hydrometricSiteNew); retStr = DoAddChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("HydrometricSites", hydrometricSiteNew.HydrometricSiteID, LogCommandEnum.Add, hydrometricSiteNew); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(GetHydrometricSiteModelWithHydrometricSiteIDDB(hydrometricSiteNew.HydrometricSiteID)); }
public IHttpActionResult GetHydrometricSiteWithID([FromUri] int HydrometricSiteID, [FromUri] string lang = "en", [FromUri] string extra = "") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); hydrometricSiteService.Query = hydrometricSiteService.FillQuery(typeof(HydrometricSite), lang, 0, 1, "", "", extra); if (hydrometricSiteService.Query.Extra == "A") { HydrometricSiteExtraA hydrometricSiteExtraA = new HydrometricSiteExtraA(); hydrometricSiteExtraA = hydrometricSiteService.GetHydrometricSiteExtraAWithHydrometricSiteID(HydrometricSiteID); if (hydrometricSiteExtraA == null) { return(NotFound()); } return(Ok(hydrometricSiteExtraA)); } else if (hydrometricSiteService.Query.Extra == "B") { HydrometricSiteExtraB hydrometricSiteExtraB = new HydrometricSiteExtraB(); hydrometricSiteExtraB = hydrometricSiteService.GetHydrometricSiteExtraBWithHydrometricSiteID(HydrometricSiteID); if (hydrometricSiteExtraB == null) { return(NotFound()); } return(Ok(hydrometricSiteExtraB)); } else { HydrometricSite hydrometricSite = new HydrometricSite(); hydrometricSite = hydrometricSiteService.GetHydrometricSiteWithHydrometricSiteID(HydrometricSiteID); if (hydrometricSite == null) { return(NotFound()); } return(Ok(hydrometricSite)); } } }
/// <summary> /// Tries to execute the CSSPDB transaction (add/delete/update) on an [HydrometricSite](CSSPModels.HydrometricSite.html) item /// </summary> /// <param name="hydrometricSite">Is the HydrometricSite item the client want to add to CSSPDB. What's important here is the HydrometricSiteID</param> /// <returns>true if HydrometricSite item was updated to CSSPDB, false if an error happened during the DB requested transtaction</returns> private bool TryToSave(HydrometricSite hydrometricSite) { try { db.SaveChanges(); } catch (DbUpdateException ex) { hydrometricSite.ValidationResults = new List <ValidationResult>() { new ValidationResult(ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")) }.AsEnumerable(); return(false); } return(true); }
/// <summary> /// Updates an [HydrometricSite](CSSPModels.HydrometricSite.html) item in CSSPDB /// </summary> /// <param name="hydrometricSite">Is the HydrometricSite item the client want to add to CSSPDB. What's important here is the HydrometricSiteID</param> /// <returns>true if HydrometricSite item was updated to CSSPDB, false if an error happened during the DB requested transtaction</returns> public bool Update(HydrometricSite hydrometricSite) { hydrometricSite.ValidationResults = Validate(new ValidationContext(hydrometricSite), ActionDBTypeEnum.Update); if (hydrometricSite.ValidationResults.Count() > 0) { return(false); } db.HydrometricSites.Update(hydrometricSite); if (!TryToSave(hydrometricSite)) { return(false); } return(true); }
public HydrometricSiteModel PostDeleteHydrometricSiteDB(int HydrometricSiteID) { ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } HydrometricSite hydrometricSiteToDelete = GetHydrometricSiteWithHydrometricSiteIDDB(HydrometricSiteID); if (hydrometricSiteToDelete == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToDelete, ServiceRes.HydrometricSite))); } int TVItemIDToDelete = hydrometricSiteToDelete.HydrometricSiteTVItemID; using (TransactionScope ts = new TransactionScope()) { db.HydrometricSites.Remove(hydrometricSiteToDelete); string retStr = DoDeleteChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("HydrometricSites", hydrometricSiteToDelete.HydrometricSiteID, LogCommandEnum.Delete, hydrometricSiteToDelete); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } TVItemModel tvItemModelRet = _TVItemService.PostDeleteTVItemWithTVItemIDDB(TVItemIDToDelete); if (!string.IsNullOrWhiteSpace(tvItemModelRet.Error)) { return(ReturnError(tvItemModelRet.Error)); } ts.Complete(); } return(ReturnError("")); }
public void HydrometricSite_Controller_GetHydrometricSiteWithID_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { HydrometricSiteController hydrometricSiteController = new HydrometricSiteController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(hydrometricSiteController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, hydrometricSiteController.DatabaseType); HydrometricSite hydrometricSiteFirst = new HydrometricSite(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(new Query(), db, ContactID); hydrometricSiteFirst = (from c in db.HydrometricSites select c).FirstOrDefault(); } // ok with HydrometricSite info IHttpActionResult jsonRet = hydrometricSiteController.GetHydrometricSiteWithID(hydrometricSiteFirst.HydrometricSiteID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <HydrometricSite> Ret = jsonRet as OkNegotiatedContentResult <HydrometricSite>; HydrometricSite hydrometricSiteRet = Ret.Content; Assert.AreEqual(hydrometricSiteFirst.HydrometricSiteID, hydrometricSiteRet.HydrometricSiteID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Not Found IHttpActionResult jsonRet2 = hydrometricSiteController.GetHydrometricSiteWithID(0); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <HydrometricSite> hydrometricSiteRet2 = jsonRet2 as OkNegotiatedContentResult <HydrometricSite>; Assert.IsNull(hydrometricSiteRet2); NotFoundResult notFoundRequest = jsonRet2 as NotFoundResult; Assert.IsNotNull(notFoundRequest); } } }
public IHttpActionResult Delete([FromBody] HydrometricSite hydrometricSite, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!hydrometricSiteService.Delete(hydrometricSite)) { return(BadRequest(String.Join("|||", hydrometricSite.ValidationResults))); } else { hydrometricSite.ValidationResults = null; return(Ok(hydrometricSite)); } } }
// Fill public string FillHydrometricSite(HydrometricSite hydrometricSiteNew, HydrometricSiteModel hydrometricSiteModel, ContactOK contactOK) { hydrometricSiteNew.DBCommand = (int)hydrometricSiteModel.DBCommand; hydrometricSiteNew.HydrometricSiteTVItemID = hydrometricSiteModel.HydrometricSiteTVItemID; hydrometricSiteNew.FedSiteNumber = hydrometricSiteModel.FedSiteNumber; hydrometricSiteNew.QuebecSiteNumber = hydrometricSiteModel.QuebecSiteNumber; hydrometricSiteNew.HydrometricSiteName = hydrometricSiteModel.HydrometricSiteName; hydrometricSiteNew.Description = hydrometricSiteModel.Description; hydrometricSiteNew.Province = hydrometricSiteModel.Province; hydrometricSiteNew.Elevation_m = hydrometricSiteModel.Elevation_m; hydrometricSiteNew.Elevation_m = hydrometricSiteModel.Elevation_m; hydrometricSiteNew.StartDate_Local = hydrometricSiteModel.StartDate_Local; hydrometricSiteNew.EndDate_Local = hydrometricSiteModel.EndDate_Local; hydrometricSiteNew.TimeOffset_hour = hydrometricSiteModel.TimeOffset_hour; hydrometricSiteNew.DrainageArea_km2 = hydrometricSiteModel.DrainageArea_km2; hydrometricSiteNew.IsNatural = hydrometricSiteModel.IsNatural; hydrometricSiteNew.IsActive = hydrometricSiteModel.IsActive; hydrometricSiteNew.Sediment = hydrometricSiteModel.Sediment; hydrometricSiteNew.RHBN = hydrometricSiteModel.RHBN; hydrometricSiteNew.Province = hydrometricSiteModel.Province; hydrometricSiteNew.RealTime = hydrometricSiteModel.RealTime; hydrometricSiteNew.HasDischarge = hydrometricSiteModel.HasDischarge; hydrometricSiteNew.HasLevel = hydrometricSiteModel.HasLevel; hydrometricSiteNew.HasRatingCurve = hydrometricSiteModel.HasRatingCurve; hydrometricSiteNew.TimeOffset_hour = hydrometricSiteModel.TimeOffset_hour; hydrometricSiteNew.LastUpdateDate_UTC = DateTime.UtcNow; if (contactOK == null) { hydrometricSiteNew.LastUpdateContactTVItemID = 2; } else { hydrometricSiteNew.LastUpdateContactTVItemID = contactOK.ContactTVItemID; } return(""); }
/// <summary> /// Validate function for all HydrometricSiteService 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); HydrometricSite hydrometricSite = validationContext.ObjectInstance as HydrometricSite; hydrometricSite.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (hydrometricSite.HydrometricSiteID == 0) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "HydrometricSiteID"), new[] { "HydrometricSiteID" })); } if (!(from c in db.HydrometricSites select c).Where(c => c.HydrometricSiteID == hydrometricSite.HydrometricSiteID).Any()) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "HydrometricSite", "HydrometricSiteID", hydrometricSite.HydrometricSiteID.ToString()), new[] { "HydrometricSiteID" })); } } TVItem TVItemHydrometricSiteTVItemID = (from c in db.TVItems where c.TVItemID == hydrometricSite.HydrometricSiteTVItemID select c).FirstOrDefault(); if (TVItemHydrometricSiteTVItemID == null) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "HydrometricSiteTVItemID", hydrometricSite.HydrometricSiteTVItemID.ToString()), new[] { "HydrometricSiteTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.HydrometricSite, }; if (!AllowableTVTypes.Contains(TVItemHydrometricSiteTVItemID.TVType)) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsNotOfType_, "HydrometricSiteTVItemID", "HydrometricSite"), new[] { "HydrometricSiteTVItemID" })); } } if (!string.IsNullOrWhiteSpace(hydrometricSite.FedSiteNumber) && hydrometricSite.FedSiteNumber.Length > 7) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "FedSiteNumber", "7"), new[] { "FedSiteNumber" })); } if (!string.IsNullOrWhiteSpace(hydrometricSite.QuebecSiteNumber) && hydrometricSite.QuebecSiteNumber.Length > 7) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "QuebecSiteNumber", "7"), new[] { "QuebecSiteNumber" })); } if (string.IsNullOrWhiteSpace(hydrometricSite.HydrometricSiteName)) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "HydrometricSiteName"), new[] { "HydrometricSiteName" })); } if (!string.IsNullOrWhiteSpace(hydrometricSite.HydrometricSiteName) && hydrometricSite.HydrometricSiteName.Length > 200) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "HydrometricSiteName", "200"), new[] { "HydrometricSiteName" })); } if (!string.IsNullOrWhiteSpace(hydrometricSite.Description) && hydrometricSite.Description.Length > 200) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "Description", "200"), new[] { "Description" })); } if (string.IsNullOrWhiteSpace(hydrometricSite.Province)) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "Province"), new[] { "Province" })); } if (!string.IsNullOrWhiteSpace(hydrometricSite.Province) && hydrometricSite.Province.Length > 4) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "Province", "4"), new[] { "Province" })); } if (hydrometricSite.Elevation_m != null) { if (hydrometricSite.Elevation_m < 0 || hydrometricSite.Elevation_m > 10000) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "Elevation_m", "0", "10000"), new[] { "Elevation_m" })); } } if (hydrometricSite.StartDate_Local != null && ((DateTime)hydrometricSite.StartDate_Local).Year < 1849) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "StartDate_Local", "1849"), new[] { "StartDate_Local" })); } if (hydrometricSite.EndDate_Local != null && ((DateTime)hydrometricSite.EndDate_Local).Year < 1849) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "EndDate_Local", "1849"), new[] { "EndDate_Local" })); } if (hydrometricSite.StartDate_Local > hydrometricSite.EndDate_Local) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._DateIsBiggerThan_, "EndDate_Local", "HydrometricSiteStartDate_Local"), new[] { "EndDate_Local" })); } if (hydrometricSite.TimeOffset_hour != null) { if (hydrometricSite.TimeOffset_hour < -10 || hydrometricSite.TimeOffset_hour > 0) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "TimeOffset_hour", "-10", "0"), new[] { "TimeOffset_hour" })); } } if (hydrometricSite.DrainageArea_km2 != null) { if (hydrometricSite.DrainageArea_km2 < 0 || hydrometricSite.DrainageArea_km2 > 1000000) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "DrainageArea_km2", "0", "1000000"), new[] { "DrainageArea_km2" })); } } if (hydrometricSite.LastUpdateDate_UTC.Year == 1) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (hydrometricSite.LastUpdateDate_UTC.Year < 1980) { hydrometricSite.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 == hydrometricSite.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { hydrometricSite.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", hydrometricSite.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { hydrometricSite.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 { hydrometricSite.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType) { string retStr = ""; Enums enums = new Enums(LanguageRequest); RatingCurve ratingCurve = validationContext.ObjectInstance as RatingCurve; ratingCurve.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (ratingCurve.RatingCurveID == 0) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "RatingCurveID"), new[] { "RatingCurveID" })); } if (!(from c in db.RatingCurves select c).Where(c => c.RatingCurveID == ratingCurve.RatingCurveID).Any()) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "RatingCurve", "RatingCurveID", ratingCurve.RatingCurveID.ToString()), new[] { "RatingCurveID" })); } } HydrometricSite HydrometricSiteHydrometricSiteID = (from c in db.HydrometricSites where c.HydrometricSiteID == ratingCurve.HydrometricSiteID select c).FirstOrDefault(); if (HydrometricSiteHydrometricSiteID == null) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "HydrometricSite", "HydrometricSiteID", ratingCurve.HydrometricSiteID.ToString()), new[] { "HydrometricSiteID" })); } if (string.IsNullOrWhiteSpace(ratingCurve.RatingCurveNumber)) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "RatingCurveNumber"), new[] { "RatingCurveNumber" })); } if (!string.IsNullOrWhiteSpace(ratingCurve.RatingCurveNumber) && ratingCurve.RatingCurveNumber.Length > 50) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "RatingCurveNumber", "50"), new[] { "RatingCurveNumber" })); } if (ratingCurve.LastUpdateDate_UTC.Year == 1) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (ratingCurve.LastUpdateDate_UTC.Year < 1980) { ratingCurve.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 == ratingCurve.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { ratingCurve.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", ratingCurve.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { ratingCurve.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 { ratingCurve.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
public void HydrometricSite_Controller_GetHydrometricSiteList_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { HydrometricSiteController hydrometricSiteController = new HydrometricSiteController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(hydrometricSiteController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, hydrometricSiteController.DatabaseType); HydrometricSite hydrometricSiteFirst = new HydrometricSite(); int count = -1; Query query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(query, db, ContactID); hydrometricSiteFirst = (from c in db.HydrometricSites select c).FirstOrDefault(); count = (from c in db.HydrometricSites select c).Count(); count = (query.Take > count ? count : query.Take); } // ok with HydrometricSite info IHttpActionResult jsonRet = hydrometricSiteController.GetHydrometricSiteList(); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <List <HydrometricSite> > ret = jsonRet as OkNegotiatedContentResult <List <HydrometricSite> >; Assert.AreEqual(hydrometricSiteFirst.HydrometricSiteID, ret.Content[0].HydrometricSiteID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); List <HydrometricSite> hydrometricSiteList = new List <HydrometricSite>(); count = -1; query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(query, db, ContactID); hydrometricSiteList = (from c in db.HydrometricSites select c).OrderBy(c => c.HydrometricSiteID).Skip(0).Take(2).ToList(); count = (from c in db.HydrometricSites select c).Count(); } if (count > 0) { query.Skip = 0; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with HydrometricSite info jsonRet = hydrometricSiteController.GetHydrometricSiteList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet); ret = jsonRet as OkNegotiatedContentResult <List <HydrometricSite> >; Assert.AreEqual(hydrometricSiteList[0].HydrometricSiteID, ret.Content[0].HydrometricSiteID); 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 HydrometricSite info IHttpActionResult jsonRet2 = hydrometricSiteController.GetHydrometricSiteList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <List <HydrometricSite> > ret2 = jsonRet2 as OkNegotiatedContentResult <List <HydrometricSite> >; Assert.AreEqual(hydrometricSiteList[1].HydrometricSiteID, ret2.Content[0].HydrometricSiteID); Assert.AreEqual((count > query.Take ? query.Take : count), ret2.Content.Count); } } } } }
public HydrometricSiteModel PostUpdateHydrometricSiteDB(HydrometricSiteModel hydrometricSiteModel) { string retStr = HydrometricSiteModelOK(hydrometricSiteModel); if (!string.IsNullOrEmpty(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } HydrometricSite hydrometricSiteToUpdate = GetHydrometricSiteWithHydrometricSiteIDDB(hydrometricSiteModel.HydrometricSiteID); if (hydrometricSiteToUpdate == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToUpdate, ServiceRes.HydrometricSite))); } retStr = FillHydrometricSite(hydrometricSiteToUpdate, hydrometricSiteModel, 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("HydrometricSites", hydrometricSiteToUpdate.HydrometricSiteID, LogCommandEnum.Change, hydrometricSiteToUpdate); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } foreach (LanguageEnum Lang in LanguageListAllowable) { if (Lang == LanguageRequest) { TVItemLanguageModel tvItemLanguageModelToUpdate = _TVItemService._TVItemLanguageService.GetTVItemLanguageModelWithTVItemIDAndLanguageDB(hydrometricSiteToUpdate.HydrometricSiteTVItemID, Lang); if (!string.IsNullOrWhiteSpace(tvItemLanguageModelToUpdate.Error)) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_With_Equal_, ServiceRes.TVItemLanguage, ServiceRes.TVItemID + "," + ServiceRes.Language, hydrometricSiteToUpdate.HydrometricSiteTVItemID.ToString() + "," + Lang))); } tvItemLanguageModelToUpdate.TVText = CreateTVText(hydrometricSiteModel); if (string.IsNullOrWhiteSpace(tvItemLanguageModelToUpdate.TVText)) { return(ReturnError(string.Format(ServiceRes._IsRequired, ServiceRes.TVText))); } TVItemLanguageModel tvItemLanguageModel = _TVItemService._TVItemLanguageService.PostUpdateTVItemLanguageDB(tvItemLanguageModelToUpdate); if (!string.IsNullOrWhiteSpace(tvItemLanguageModel.Error)) { return(ReturnError(tvItemLanguageModel.Error)); } } } ts.Complete(); } return(GetHydrometricSiteModelWithHydrometricSiteIDDB(hydrometricSiteToUpdate.HydrometricSiteID)); }
private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType) { string retStr = ""; Enums enums = new Enums(LanguageRequest); HydrometricDataValue hydrometricDataValue = validationContext.ObjectInstance as HydrometricDataValue; hydrometricDataValue.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (hydrometricDataValue.HydrometricDataValueID == 0) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "HydrometricDataValueID"), new[] { "HydrometricDataValueID" })); } if (!(from c in db.HydrometricDataValues select c).Where(c => c.HydrometricDataValueID == hydrometricDataValue.HydrometricDataValueID).Any()) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "HydrometricDataValue", "HydrometricDataValueID", hydrometricDataValue.HydrometricDataValueID.ToString()), new[] { "HydrometricDataValueID" })); } } HydrometricSite HydrometricSiteHydrometricSiteID = (from c in db.HydrometricSites where c.HydrometricSiteID == hydrometricDataValue.HydrometricSiteID select c).FirstOrDefault(); if (HydrometricSiteHydrometricSiteID == null) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "HydrometricSite", "HydrometricSiteID", hydrometricDataValue.HydrometricSiteID.ToString()), new[] { "HydrometricSiteID" })); } if (hydrometricDataValue.DateTime_Local.Year == 1) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "DateTime_Local"), new[] { "DateTime_Local" })); } else { if (hydrometricDataValue.DateTime_Local.Year < 1980) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "DateTime_Local", "1980"), new[] { "DateTime_Local" })); } } retStr = enums.EnumTypeOK(typeof(StorageDataTypeEnum), (int?)hydrometricDataValue.StorageDataType); if (!string.IsNullOrWhiteSpace(retStr)) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "StorageDataType"), new[] { "StorageDataType" })); } if (hydrometricDataValue.Discharge_m3_s != null) { if (hydrometricDataValue.Discharge_m3_s < 0 || hydrometricDataValue.Discharge_m3_s > 100000) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "Discharge_m3_s", "0", "100000"), new[] { "Discharge_m3_s" })); } } if (hydrometricDataValue.DischargeEntered_m3_s != null) { if (hydrometricDataValue.DischargeEntered_m3_s < 0 || hydrometricDataValue.DischargeEntered_m3_s > 100000) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "DischargeEntered_m3_s", "0", "100000"), new[] { "DischargeEntered_m3_s" })); } } if (hydrometricDataValue.Level_m != null) { if (hydrometricDataValue.Level_m < 0 || hydrometricDataValue.Level_m > 10000) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "Level_m", "0", "10000"), new[] { "Level_m" })); } } //HourlyValues has no StringLength Attribute if (hydrometricDataValue.LastUpdateDate_UTC.Year == 1) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (hydrometricDataValue.LastUpdateDate_UTC.Year < 1980) { hydrometricDataValue.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 == hydrometricDataValue.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", hydrometricDataValue.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { hydrometricDataValue.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 { hydrometricDataValue.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
public HydrometricSiteTest() { hydrometricSite = new HydrometricSite(); hydrometricSiteExtraA = new HydrometricSiteExtraA(); hydrometricSiteExtraB = new HydrometricSiteExtraB(); }
public void HydrometricSite_Controller_Post_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { HydrometricSiteController hydrometricSiteController = new HydrometricSiteController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(hydrometricSiteController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, hydrometricSiteController.DatabaseType); HydrometricSite hydrometricSiteLast = new HydrometricSite(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; query.Asc = ""; query.Desc = ""; HydrometricSiteService hydrometricSiteService = new HydrometricSiteService(query, db, ContactID); hydrometricSiteLast = (from c in db.HydrometricSites select c).FirstOrDefault(); } // ok with HydrometricSite info IHttpActionResult jsonRet = hydrometricSiteController.GetHydrometricSiteWithID(hydrometricSiteLast.HydrometricSiteID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <HydrometricSite> Ret = jsonRet as OkNegotiatedContentResult <HydrometricSite>; HydrometricSite hydrometricSiteRet = Ret.Content; Assert.AreEqual(hydrometricSiteLast.HydrometricSiteID, hydrometricSiteRet.HydrometricSiteID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Post to return CSSPError because HydrometricSiteID exist IHttpActionResult jsonRet2 = hydrometricSiteController.Post(hydrometricSiteRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <HydrometricSite> hydrometricSiteRet2 = jsonRet2 as OkNegotiatedContentResult <HydrometricSite>; Assert.IsNull(hydrometricSiteRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest2); // Post to return newly added HydrometricSite hydrometricSiteRet.HydrometricSiteID = 0; hydrometricSiteController.Request = new System.Net.Http.HttpRequestMessage(); hydrometricSiteController.Request.RequestUri = new System.Uri("http://localhost:5000/api/hydrometricSite"); IHttpActionResult jsonRet3 = hydrometricSiteController.Post(hydrometricSiteRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); CreatedNegotiatedContentResult <HydrometricSite> hydrometricSiteRet3 = jsonRet3 as CreatedNegotiatedContentResult <HydrometricSite>; Assert.IsNotNull(hydrometricSiteRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNull(badRequest3); IHttpActionResult jsonRet4 = hydrometricSiteController.Delete(hydrometricSiteRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet4); OkNegotiatedContentResult <HydrometricSite> hydrometricSiteRet4 = jsonRet4 as OkNegotiatedContentResult <HydrometricSite>; Assert.IsNotNull(hydrometricSiteRet4); BadRequestErrorMessageResult badRequest4 = jsonRet4 as BadRequestErrorMessageResult; Assert.IsNull(badRequest4); } } }