@@ -1926,4 +1926,230 @@ public override ODataAPIResponseStatus TryAddRelatedObject(IEdmStructuredObject
19261926 return ODataAPIResponseStatus . Success ;
19271927 }
19281928 }
1929+
1930+ internal class StudentAPIHandler : ODataAPIHandler < Student >
1931+ {
1932+ public override ODataAPIResponseStatus TryCreate ( IDictionary < string , object > keyValues , out Student createdObject , out string errorMessage )
1933+ {
1934+ createdObject = null ;
1935+ errorMessage = string . Empty ;
1936+
1937+ try
1938+ {
1939+ createdObject = new Student ( ) ;
1940+ StudentController . Students . Add ( createdObject ) ;
1941+
1942+ return ODataAPIResponseStatus . Success ;
1943+ }
1944+ catch ( Exception ex )
1945+ {
1946+ errorMessage = ex . Message ;
1947+
1948+ return ODataAPIResponseStatus . Failure ;
1949+ }
1950+ }
1951+
1952+ public override ODataAPIResponseStatus TryDelete ( IDictionary < string , object > keyValues , out string errorMessage )
1953+ {
1954+ errorMessage = string . Empty ;
1955+
1956+ try
1957+ {
1958+ var id = keyValues . First ( ) . Value . ToString ( ) ;
1959+ var student = StudentController . Students . First ( x => x . Id == Int32 . Parse ( id ) ) ;
1960+
1961+ StudentController . Students . Remove ( student ) ;
1962+
1963+ return ODataAPIResponseStatus . Success ;
1964+ }
1965+ catch ( Exception ex )
1966+ {
1967+ errorMessage = ex . Message ;
1968+
1969+ return ODataAPIResponseStatus . Failure ;
1970+ }
1971+ }
1972+
1973+ public override ODataAPIResponseStatus TryGet ( IDictionary < string , object > keyValues , out Student originalObject , out string errorMessage )
1974+ {
1975+ ODataAPIResponseStatus status = ODataAPIResponseStatus . Success ;
1976+ errorMessage = string . Empty ;
1977+ originalObject = null ;
1978+
1979+ try
1980+ {
1981+ var id = keyValues [ "Id" ] . ToString ( ) ;
1982+ originalObject = StudentController . Students . FirstOrDefault ( x => x . Id == Int32 . Parse ( id ) ) ;
1983+
1984+ if ( originalObject == null )
1985+ {
1986+ status = ODataAPIResponseStatus . NotFound ;
1987+ }
1988+ }
1989+ catch ( Exception ex )
1990+ {
1991+ status = ODataAPIResponseStatus . Failure ;
1992+ errorMessage = ex . Message ;
1993+ }
1994+
1995+ return status ;
1996+ }
1997+
1998+ public override IODataAPIHandler GetNestedHandler ( Student parent , string navigationPropertyName )
1999+ {
2000+ switch ( navigationPropertyName )
2001+ {
2002+ case "Courses" :
2003+ return new CourseAPIHandler ( parent ) ;
2004+ default :
2005+ return null ;
2006+ }
2007+ }
2008+
2009+ public override ODataAPIResponseStatus TryAddRelatedObject ( Student resource , out string errorMessage )
2010+ {
2011+ errorMessage = string . Empty ;
2012+
2013+ return ODataAPIResponseStatus . Success ;
2014+ }
2015+
2016+ public override Task < ODataAPIResponseStatus > TryCreateAsync ( IDictionary < string , object > keyValues , out Student createdObject , out string errorMessage )
2017+ {
2018+ throw new NotImplementedException ( ) ;
2019+ }
2020+
2021+ public override Task < ODataAPIResponseStatus > TryGetAsync ( IDictionary < string , object > keyValues , out Student originalObject , out string errorMessage )
2022+ {
2023+ throw new NotImplementedException ( ) ;
2024+ }
2025+
2026+ public override Task < ODataAPIResponseStatus > TryDeleteAsync ( IDictionary < string , object > keyValues , out string errorMessage )
2027+ {
2028+ throw new NotImplementedException ( ) ;
2029+ }
2030+
2031+ public override Task < ODataAPIResponseStatus > TryAddRelatedObjectAsync ( Student resource , out string errorMessage )
2032+ {
2033+ throw new NotImplementedException ( ) ;
2034+ }
2035+
2036+ public override IODataAPIHandler GetNestedHandlerAsync ( Student parent , string navigationPropertyName )
2037+ {
2038+ throw new NotImplementedException ( ) ;
2039+ }
2040+ }
2041+
2042+ internal class CourseAPIHandler : ODataAPIHandler < Course >
2043+ {
2044+ Student student ;
2045+ public CourseAPIHandler ( Student student )
2046+ {
2047+ this . student = student ;
2048+ }
2049+
2050+ public override ODataAPIResponseStatus TryCreate ( IDictionary < string , object > keyValues , out Course createdObject , out string errorMessage )
2051+ {
2052+ createdObject = null ;
2053+ errorMessage = string . Empty ;
2054+
2055+ try
2056+ {
2057+ createdObject = new Course ( ) ;
2058+ StudentController . Courses . Add ( createdObject ) ;
2059+
2060+ return ODataAPIResponseStatus . Success ;
2061+ }
2062+ catch ( Exception ex )
2063+ {
2064+ errorMessage = ex . Message ;
2065+
2066+ return ODataAPIResponseStatus . Failure ;
2067+ }
2068+ }
2069+
2070+ public override ODataAPIResponseStatus TryDelete ( IDictionary < string , object > keyValues , out string errorMessage )
2071+ {
2072+ errorMessage = string . Empty ;
2073+
2074+ try
2075+ {
2076+ var id = keyValues . First ( ) . Value . ToString ( ) ;
2077+ var course = StudentController . Courses . First ( x => x . Id == Int32 . Parse ( id ) ) ;
2078+
2079+ StudentController . Courses . Remove ( course ) ;
2080+
2081+ return ODataAPIResponseStatus . Success ;
2082+ }
2083+ catch ( Exception ex )
2084+ {
2085+ errorMessage = ex . Message ;
2086+
2087+ return ODataAPIResponseStatus . Failure ;
2088+ }
2089+ }
2090+
2091+ public override ODataAPIResponseStatus TryGet ( IDictionary < string , object > keyValues , out Course originalObject , out string errorMessage )
2092+ {
2093+ ODataAPIResponseStatus status = ODataAPIResponseStatus . Success ;
2094+ errorMessage = string . Empty ;
2095+ originalObject = null ;
2096+
2097+ try
2098+ {
2099+ var id = keyValues [ "Id" ] . ToString ( ) ;
2100+ originalObject = StudentController . Courses . FirstOrDefault ( x => x . Id == Int32 . Parse ( id ) ) ;
2101+
2102+ if ( originalObject == null )
2103+ {
2104+ status = ODataAPIResponseStatus . NotFound ;
2105+ }
2106+ }
2107+ catch ( Exception ex )
2108+ {
2109+ status = ODataAPIResponseStatus . Failure ;
2110+ errorMessage = ex . Message ;
2111+ }
2112+
2113+ return status ;
2114+ }
2115+
2116+ public override IODataAPIHandler GetNestedHandler ( Course parent , string navigationPropertyName )
2117+ {
2118+ return null ;
2119+ }
2120+
2121+ public override ODataAPIResponseStatus TryAddRelatedObject ( Course resource , out string errorMessage )
2122+ {
2123+ errorMessage = string . Empty ;
2124+
2125+ this . student . Courses . Add ( resource ) ;
2126+
2127+ return ODataAPIResponseStatus . Success ;
2128+ }
2129+
2130+ public override Task < ODataAPIResponseStatus > TryCreateAsync ( IDictionary < string , object > keyValues , out Course createdObject , out string errorMessage )
2131+ {
2132+ throw new NotImplementedException ( ) ;
2133+ }
2134+
2135+ public override Task < ODataAPIResponseStatus > TryGetAsync ( IDictionary < string , object > keyValues , out Course originalObject , out string errorMessage )
2136+ {
2137+ throw new NotImplementedException ( ) ;
2138+ }
2139+
2140+ public override Task < ODataAPIResponseStatus > TryDeleteAsync ( IDictionary < string , object > keyValues , out string errorMessage )
2141+ {
2142+ throw new NotImplementedException ( ) ;
2143+ }
2144+
2145+ public override Task < ODataAPIResponseStatus > TryAddRelatedObjectAsync ( Course resource , out string errorMessage )
2146+ {
2147+ throw new NotImplementedException ( ) ;
2148+ }
2149+
2150+ public override IODataAPIHandler GetNestedHandlerAsync ( Course parent , string navigationPropertyName )
2151+ {
2152+ throw new NotImplementedException ( ) ;
2153+ }
2154+ }
19292155}
0 commit comments