public ActionResult <dynamic> Put(string ApiKey, string EntityType, [FromBody] dynamic Entity) { try { if (!WorkeFunctions.IsValidApiKey(ApiKey)) { throw new InvalidApiKeyException(); } object objectToStore; if (Entity.GetType().Name == "JObject") { var typedObject = EntityFactory.GetEmptyTypedObect(EntityType); objectToStore = EntityFactory.CopyPropertiesFromDynamicObjectToTypedObject(Entity, typedObject); } else { objectToStore = Entity; } UpdateEntityResult ret = WorkeFunctions.BusinessObjectAccess.UpdateEntity(objectToStore); if (ret == UpdateEntityResult.Success) { EntityDescriber edd = new EntityDescriber(objectToStore); if (edd.IdField() != null) { return(CreatedAtAction(nameof(Get), new { id = EntityFactory.GetPropertyByName(objectToStore, edd.IdField().Name) }, objectToStore)); } else { return(CreatedAtAction(nameof(Get), new { }, objectToStore)); } } else if (ret == UpdateEntityResult.NoRowsAffected) { return(NotFound()); } else { return(new UnprocessableEntityObjectResult(objectToStore)); } } catch (InvalidApiKeyException) { return(Unauthorized()); } catch (Alchemint.Core.Exceptions.RecordUpdateException ex) { HandleException(ex); throw ex; } catch (Exception ex) { HandleException(ex); throw ex; } }
public ActionResult <dynamic> GetById(string Id) { var obj = EntityFactory.GetEmptyTypedObect(typeof(T).Name); EntityDescriber ed = new EntityDescriber(obj); if (ed.IdField() != null) { EntityFactory.SetPropertyByName(obj, ed.IdField().Name, Id); return(_fab.Get("APIKEY", typeof(T).Name, $"{ed.IdField().Name}={Id}")); } else { throw new Exception($"{typeof(T).Name} does not have a PrimaryKey defined"); } }
private static string GetQueryStringForEntity(object Entity, List <string> propertiesToFilterOn) { EntityDescriber describer = new EntityDescriber(Entity); var properties = describer.AllPropertyValues(); string queryString = "Query="; int i = 0; bool filterSpecified = false; if (propertiesToFilterOn != null) { filterSpecified = true; } foreach (var property in properties) { var value = property.Value; bool includeThisProperty = false; if (filterSpecified) { includeThisProperty = propertiesToFilterOn.Contains(property.Name); } else { includeThisProperty = true; } if (value != null && includeThisProperty) { queryString += i > 0 ? "," : ""; queryString += $"{property.Name}={value}"; i++; } } return(queryString); }
public ActionResult <dynamic> Post(string ApiKey, string EntityType, [FromBody] dynamic Entity) { try { if (!WorkeFunctions.IsValidApiKey(ApiKey)) { throw new InvalidApiKeyException(); } var typedObject = EntityFactory.GetEmptyTypedObect(EntityType); EntityDescriber ed = new EntityDescriber(typedObject); var fld = ed.IdField(); if (fld != null) { if (fld.Type.Name.ToUpper() == "STRING") { if (Entity.GetType().Name == "JObject") { if (Entity[fld.Name] == null) { Entity[fld.Name] = Guid.NewGuid().ToString(); } else if (Entity[fld.Name].ToString().Trim().Length <= 0) { Entity[fld.Name] = Guid.NewGuid().ToString(); } } else { var idValue = EntityFactory.GetPropertyByName(Entity, fld.Name); if (idValue == null) { EntityFactory.SetPropertyByName(Entity, fld.Name, Guid.NewGuid().ToString()); } else if (idValue.ToString().Trim().Length <= 0) { EntityFactory.SetPropertyByName(Entity, fld.Name, Guid.NewGuid().ToString()); } } } } dynamic objectToStore; if (Entity.GetType().Name == "JObject") { objectToStore = EntityFactory.CopyPropertiesFromDynamicObjectToTypedObject(Entity, typedObject); } else { objectToStore = Entity; } CreateEntityResult ret = WorkeFunctions.BusinessObjectAccess.StoreEntity(objectToStore); if (ret == CreateEntityResult.Success) { return(CreatedAtAction(nameof(Get), new { id = objectToStore.Id }, objectToStore)); } else if (ret == CreateEntityResult.EntityWithPrimaryKeyRecordExists || ret == CreateEntityResult.EntityWithUniqueKeyRecordExists) { return(Conflict()); } else { return(new UnprocessableEntityObjectResult(objectToStore)); } } catch (InvalidApiKeyException) { return(Unauthorized()); } catch (Alchemint.Core.Exceptions.RecordCreationException ex) { HandleException(ex); throw ex; } catch (Exception ex) { HandleException(ex); throw ex; } }