/// <summary> /// Inserts a new gadget into the database /// </summary> /// <param name="data"></param> /// <returns></returns> public ObjectReturnData <Gadget2> InsertGadget(GadgetInsertData data) { try { //Insert row into database and read back object int id = _dataAccess.Insert <int, GadgetInsertData>(data, StoredProcedureTypes.Insert); Gadget gadget = _dataAccess.GetObjectById <int, Gadget>(id, StoredProcedureTypes.ById); Gadget2 g2 = new Gadget2(); g2.Populate(gadget); ObjectReturnData <Gadget2> returnObject = new ObjectReturnData <Gadget2> { Id = id.ToString(), //Easier to deal with Id as a string in javascript Value = g2, // Adds UpdateDateTimeString so javascript doesn't have to convert the time to a string IsSuccessful = true }; return(returnObject); } catch (Exception ex) { ObjectReturnData <Gadget2> returnObject = new ObjectReturnData <Gadget2>() { //In produciton code ex.ToString() should be logged and a more user friend error message should be returned ErrorMessage = ex.ToString(), CallingMethod = (new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name, //method that called this method IsSuccessful = false }; return(returnObject); } }
/// <summary> /// Returns all non deleted gadgets /// </summary> /// <returns></returns> public static ListReturnData <Gadget2> GetGadgets() { try { //Get list of gadgets from the database IDataAccess dataAccess = DataAccessCreator.CreateDataAccess(); List <Gadget> gadgetList = dataAccess.GetList <Gadget>(StoredProcedureTypes.List); //Make new list of objects with additional UpdateDateTimeString field and populate List <Gadget2> gadgetList2 = new List <Gadget2>(); gadgetList.ForEach(gadget => { Gadget2 g2 = new Gadget2(); g2.Populate(gadget); gadgetList2.Add(g2); }); ListReturnData <Gadget2> returnObject = new ListReturnData <Gadget2> { List = gadgetList2, IsSuccessful = true }; return(returnObject); } catch (Exception ex) { ListReturnData <Gadget2> returnObject = new ListReturnData <Gadget2>() { //In produciton code ex.ToString() should be logged and a more user friend error message should be returned ErrorMessage = ex.ToString(), CallingMethod = (new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name, //method that called this method IsSuccessful = false }; return(returnObject); } }