/// <summary> /// The BeforeSave callback will set the CreatedAt date on creation and the ModifiedAt date on updates. /// </summary> /// <param name="toSave">The Poco object that is being saved</param> /// <param name="parameters">Unused for now</param> /// <returns>returns true</returns> public override bool?BeforeSave(Poco toSave, BusinessParameters parameters) { if (toSave.ID == null) { //The object is being created so set the CreatedAt date. toSave.ModifiedAt = null; toSave.CreatedAt = DateTime.Now; } else { // The Poco is being updated // If the CreatedAt date in the Poco is null, fetch the correct //date from the Db so CreatedAt isn't set to null in the db. if (toSave.CreatedAt == null) { toSave.CreatedAt = (from m in business.Table where m.ID == toSave.ID select m.CreatedAt).First(); } toSave.ModifiedAt = DateTime.Now; } return(true); }
public async Task <PageList <Business> > GetAllBusinesses(BusinessParameters businessParameters) { return(await PageList <Business> .ToPageList(GetAll() .Include(b => b.County) .Include(b => b.Sector) .Include(b => b.Inspections).ThenInclude(i => i.InspectionType) .Include(b => b.Inspections).ThenInclude(i => i.EnforcementAgency) .Include(b => b.Inspections).ThenInclude(i => i.InspectionGuidelines).ThenInclude(g => g.Guideline) .OrderBy(b => b.BusinessName), businessParameters.PageNumber, businessParameters.PageSize)); }
/// <summary> /// This is a convenience method to delete a row with only the ID. Useful if you have the id of the row to delete, but /// not the POCO object to delete it. GenericBusiness expect having the POCO object to delete normaly, this method allows /// you to delete by id without having to write the code to fettch the poco object yourself /// </summary> /// <param name="id"></param> /// <param name="parameters"></param> /// <returns></returns> public virtual bool Delete(int id, BusinessParameters parameters = null) { var toDelete = this.Find(id); // If the row exists, delete it through the POCO object as it normally works if (toDelete != null) { return(this.Delete(toDelete, parameters)); } return(false); }
/// <summary> /// This is a convinence method that can be usined instead of FindFirst("id"). It takes the id directly as a int and so /// there is no need to create a Businessparameters object and it streamlines the process of finding an object by /// ID /// </summary> /// <param name="id">The id representing the desired row</param> /// <param name="parameters">Can be used to further customize the find with a EFBusinessQuery, but /// in this case there usually no reason to use this parameter</param> /// <returns></returns> public virtual Poco Find(int id, BusinessParameters parameters = null) { // No BusinessParameters passed, create an empty one if (parameters == null) { parameters = new BusinessParameters(); } // Use the IdFind custom finder through FindFirst to fetch the row represented by the given IDs parameters["id"] = id; return(this.FindFirst("id", parameters)); }
/// <summary> /// The BeforeDelete callback marks the Poco as deleted and prevent the execution of the actual deletion /// </summary> /// <param name="toDelete">The Poco object that will be soft deleted</param> /// <param name="parameters">You can specify true under [soft-deletable][disable] to delete for real</param> /// <returns></returns> public override bool?BeforeDelete(Poco toDelete, BusinessParameters parameters) { if (parameters != null && (parameters.GetDeepValue("soft-deletable.disable") as bool?) == true) { return(true); } //Marks the object as deleted toDelete.DeletedAt = DateTime.Now; business.Save(toDelete); return(null); }
public IQueryable <Poco> FindBySlug(BusinessParameters parameters) { if (!parameters.Keys.Contains("slug")) { return(null); } string slug = parameters["slug"]; return(from m in this.business.Table where m.Slug == slug select m); }
/// <summary> /// In the BeforeSave callback, the slug is created and added to the Poco object that is being saved /// </summary> /// <param name="toSave">The Poco object that is being saved</param> /// <param name="parameters">Unused for now</param> /// <returns></returns> public override bool?BeforeSave(Poco toSave, BusinessParameters parameters) { // Create the slug based on the SluggedProperties toSave.Slug = ""; foreach (string propertyName in SluggedProperties) { string title = toSave.GetType().GetProperty(propertyName).GetValue(toSave).ToString(); toSave.Slug += GenerateSlug(title) + "-"; } // Remove the trailing - from the slug toSave.Slug = toSave.Slug.Remove(toSave.Slug.Length - 1); //Slugs must be unique so this loop check for duplicates and appends a number to the slug until // the slug is unique string tmpSlug = toSave.Slug; bool slugIsDuplicated; do { // Try to find a row with the current slug var alreadyUsed = (from m in business.Table where m.Slug == toSave.Slug select new { ID = m.ID, Slug = m.Slug }).FirstOrDefault(); slugIsDuplicated = alreadyUsed != null && !string.IsNullOrEmpty(alreadyUsed.Slug) && toSave.ID != alreadyUsed.ID; if (slugIsDuplicated) { var splitted = alreadyUsed.Slug.Split(new string[] { "__" }, StringSplitOptions.RemoveEmptyEntries); string endSlug = splitted.Last(); // The TryParse means the slugId will be 1 if this is the first duplicate found, // if not the first duplicate found, the slugId will have the value of endSlug int slugId = 1; int.TryParse(endSlug, out slugId); ++slugId; toSave.Slug = tmpSlug + "__" + slugId; } } while (slugIsDuplicated); return(true); }
/// <summary> /// This defines how the data is saved to the database. SaveHelper is overriden instead of Save because /// Save includes code to call the proper behavior callbacks and we do not want to lose this functionality /// </summary> /// <param name="toSave">The Poco object that will be saved</param> /// <param name="parameters">Currently unused, but could be used to customize the save /// functionality in some way</param> /// <returns>True if the save succeded, else false</returns> protected override bool SaveHelper(Poco toSave, BusinessParameters parameters = null) { dynamic toSaveTmp = toSave; // Since ID is now, this is a create operation if (toSaveTmp.ID == null) { Table.Add(toSave); Db.SaveChanges(); } // Since ID isn't null this is an update operation else { Db.Entry(toSave).State = EntityState.Modified; Db.SaveChanges(); } return(true); }
public async Task <IActionResult> GetBusinesses([FromQuery] BusinessParameters businessParameters) { try { var businesses = await _repo.Business.GetAllBusinesses(businessParameters); _logger.LogInfo("Retrived All businesses from DB. Action: GetBusinesses"); var metadata = new PaginationMetadata <Business>(businesses); Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata)); var businessesResult = _mapper.Map <IEnumerable <BusinessDto> >(businesses); return(Ok(businessesResult)); } catch (Exception ex) { _logger.LogError($"Error happened in Action: GetBusinesses. Error: {ex.Message}"); return(new ObjectResult(new InternalServerError(ex))); } }
/// <summary> /// The after find callback filters the Find results so only the rows that aren't marked as deleted are returned /// </summary> /// <param name="type">The type of find</param> /// <param name="parameters"> /// Can be used to adapt the functionality of AfterFind depending on the value of ["soft-deletable]["mode"]: /// -If null (the default) or "not-deleted", than AfterFind will only return the rows that aren't marked as deleted /// -If "only-deleted, the AfterFind will only return the rows that are marked as deleted /// -If any other value (suggested is "all"), than all rows are returned regardless of their deletion status /// </param> /// <param name="results">The result obtained by the find</param> /// <returns>The filtered out results</returns> public override IQueryable <Poco> AfterFind(string type, BusinessParameters parameters, IQueryable <Poco> results = null) { string mode = parameters != null?parameters.GetDeepValue("soft-deletable.mode") as string : null; if (mode == null) { mode = "not-deleted"; } if (mode == "not-deleted") { return(results.Where(m => m.DeletedAt == null)); } else if (mode == "only-deleted") { return(results.Where(m => m.DeletedAt != null)); } return(results); }
public virtual IQueryable <Poco> DefaultFind(BusinessParameters parameters) { IQueryable <Poco> target = Table; // If parameters contains a "query" key if (parameters != null && parameters.Keys.Contains("query")) { // Apply the passed queries to the find operation EFBusinessQuery <Poco> query = parameters["query"]; if (query.Where != null) { target = target.Where(query.Where); } if (query.OrderBy != null) { target = target.OrderBy(query.OrderBy); } } return(target); }
/// <summary> /// Actualy delete the row associated to a given id. You can use this if you need to really delete a row /// instead of just marking it as deleted /// </summary> /// <param name="toDelete">The id of the row that will be deleted</param> /// <param name="parameters">Can customize the functionality of the ForceDelete, not really useful /// for now</param> /// <returns>True if the row was deleted, else false</returns> public bool ForceDelete(int id, BusinessParameters parameters = null) { parameters = BusinessParameters.SetDeepValue(parameters, "soft-deletable.mode", "all"); return(ForceDelete(business.Find(id, parameters), parameters)); }
/// <summary> /// This defines how the data is deleted from the database. DeleteHelper is overriden instead of Delete because /// Delete includes code to call the proper behavior callbacks and we do not want to lose this functionality /// </summary> /// <param name="toDelete">The Poco object that will be deleted from the database</param> /// <param name="parameters">Currently unused, but could be used to customize the delete /// functionality in some way<</param> /// <returns>True if delete success, else false</returns> protected override bool DeleteHelper(Poco toDelete, BusinessParameters parameters = null) { Table.Remove(toDelete); Db.SaveChanges(); return(true); }
public virtual IQueryable <T> AfterFind(string type, BusinessParameters parameters, IQueryable <T> results = null) { return(results); }
public virtual bool?AfterSave(T toSave, BusinessParameters parameters) { return(true); }
public virtual bool?AfterDelete(T toDelete, BusinessParameters parameters) { return(true); }
public IQueryable <Poco> IdFind(BusinessParameters parameters) { int id = parameters["id"]; return(Table.Where(p => p.ID == id)); }
/// <summary> /// Actualy delete the row associated to a given Poco. You can use this if you need to really delete a row /// instead of just marking it as deleted /// </summary> /// <param name="toDelete">The Poco object that will be deleted</param> /// <param name="parameters">Can customize the functionality of the ForceDelete, not really useful /// for now</param> /// <returns>True if the row was deleted, else false</returns> public bool ForceDelete(Poco toDelete, BusinessParameters parameters = null) { parameters = BusinessParameters.SetDeepValue(parameters, "soft-deletable.disable", true); return(business.Delete(toDelete, parameters)); }