Please review the code and give me some suggestion to improve coding standard.
I created a simple CRUD operation using Rest Services in Cache.
Class EX.example Extends %CSP.REST { XData UrlMap { <Routes> <Route Url="/insert/:loc/:ssn/:name" Method="GET" Call="hello"/> <Route Url="/update/:key/:loc/:ssn/:name" Method="GET" Call="iam"/> <Route Url="/delete/:key" Method="GET" Call="there"/> <Route Url="/findid/:key" Method="GET" Call="now"/> </Routes> } ClassMethod hello(name As %String, ssn As %String, loc As %String) As %Status { s mName=$zcvt(name,"U"),mKey=ssn,mLoc=loc s ns=$namespace znspace "USER" try{ s obj=##class(Sample.Classes).%New() s obj.name=mName, obj.SSN=mKey, obj.Location=mLoc w "----Great----" s Status=obj.%Save() } catch { w "Sorry Unable to insert" } znspace ns quit $$$OK } ClassMethod iam(id As %Integer, name As %String, ssn As %String, loc As %String) { s mId=id,mName=$zcvt(name,"U") s ns=$namespace znspace "USER" try{ if (##class(Sample.Classes).%ExistsId(mId)) { s objj=##class(Sample.Classes).%OpenId(mId) w objj.%Id(),objj.Location s objj.Location=loc,objj.name=mName,objj.SSN=ssn s Status=objj.%Save() w "----Wow Great---------" } else { w "There is no ",id," is present" } } catch { w !,"Sorry Unable to update the Id:",mId } znspace ns q $$$OK } ClassMethod there(id As %Integer) { S mId=id s ns=$namespace znspace "USER" try{ if (##class(Sample.Classes).%ExistsId(mId)) { s obj=##class(Sample.Classes).%DeleteId(id) w "Deleted Successfully" } else { w "There is no ",id," is present" } } catch { w "Sorry Unable to delete the Id:",mId } znspace ns q $$$OK } ClassMethod now(id As %Integer) { S mId=id s ns=$namespace znspace "USER" try{ if (##class(Sample.Classes).%ExistsId(mId)) { s objj=##class(Sample.Classes).%OpenId(mId) w "ID:",objj.%Id()," Name:",objj.name," SSN:",objj.SSN," Location:",objj.Location } else { w "There is no ",id," is present" } } catch { w "Sorry Unable to find the Id:",mId } znspace ns q $$$OK } }
Created a simple class with three properties and performed the CRUD operation for below Class Definition
Class Sample.Classes Extends %Persistent { Property name As %String; Property SSN As %String; Property Location As %String; }
Please review the code and give me some suggestion to improve coding standard.
Thanks