/// <summary> /// Inserts the data into the database. /// </summary> /// <param name="db">The database connection to be used</param> /// <param name="item">Data which need to be inserted</param> /// <returns>Returns null if insert fails else returns data in DynamicDictionary</returns> public virtual ResponseModel Insert(DbConnect con, DynamicDictionary item) { ResponseModel resp = new ResponseModel(false, string.Empty); TModel model = new TModel(); try { //validate data before saving if (!IsValid((DynamicDictionary)item.Clone(), false, con)) { resp.message = "Validation failed."; resp.PushValidationErrors(ValidationErrors); return(resp); } //save if (CrudRepo.Insert(con, item)) { if (LoadItemAfterSave) { //PropertyInfo key = Models.ModelService.GetKeyPropertyInfo(item); //TKey id = Models.ModelService.ChangeType<TKey>(key.GetValue(item)); //CrudSrvc.get //item = CrudSrvc.GetItemAsModel(id); resp.data = item; } resp.success = true; resp.message = "Data added successfully."; } else { resp.message = "System Error :: DB"; } } catch (Exception ex) { Errors.Add(ex.Message); } if (resp.success == false) { resp.PushErrors(CrudRepo.Errors); resp.PushErrors(Errors); resp.PushValidationErrors(ValidationErrors); } return(resp); }
/// <summary> /// Updates the data into the database. /// </summary> /// <param name="db">The database connection to be used</param> /// <param name="id">The key value of which the data need to be updated.</param> /// <param name="item">Returns null if update fails else returns data in DynamicDictionary</returns> public virtual ResponseModel Update(DbConnect con, TKey id, DynamicDictionary item) { ResponseModel resp = new ResponseModel(false, string.Empty); PropertyInfo key = model.GetKeyPropertyInfo(); if (key == null) { throw new Exception("Primary key not defined in business object(model)."); } //validate data before saving if (!IsValid(item, true, con)) { resp.message = "Validation failed."; resp.PushValidationErrors(ValidationErrors); return(resp); } //save if (CrudRepo.Update(con, id, item)) { if (LoadItemAfterSave) { //PropertyInfo key = Models.ModelService.GetKeyPropertyInfo(item); //TKey id = Models.ModelService.ChangeType<TKey>(key.GetValue(item)); //CrudSrvc.get //item = CrudSrvc.GetItemAsModel(id); resp.data = item; } resp.success = true; resp.message = "Data updated successfully."; } else { resp.message = "Error while saving data"; } if (resp.success == false) { resp.PushErrors(CrudRepo.Errors); resp.PushErrors(Errors); resp.PushValidationErrors(ValidationErrors); } return(resp); }
public virtual ResponseModel Get(DbConnect con, TKey id) { ResponseModel resp = new ResponseModel(false, String.Empty); DynamicDictionary item = GetAsDictionary(con, id); if (item == null) { resp.message = "Unable to load data."; resp.PushErrors(Errors); } else { resp.success = true; resp.data = item; resp.message = "Data loaded successfully."; } if (resp.success == false) { resp.PushErrors(CrudRepo.Errors); resp.PushErrors(Errors); } return(resp); }