1+ using System . Runtime . Serialization ;
2+
13namespace ServiceStack . MovieRest
24{
3- using System ;
4- using System . Collections . Generic ;
5- using System . Net ;
6- using ServiceStack . Common ;
7- using ServiceStack . Common . Web ;
8- using ServiceStack . DataAnnotations ;
9- using ServiceStack . OrmLite ;
10- using ServiceStack . ServiceHost ;
11- using ServiceStack . ServiceInterface ;
12- using ServiceStack . Text ;
13-
14- /// <summary>
15- /// Define your ServiceStack web service request (i.e. Request DTO).
16- /// </summary>
17- /// <remarks>The route is defined here rather than in the AppHost.</remarks>
18- [ Api ( "GET or DELETE a single movie by Id. Use POST to create a new Movie and PUT to update it" ) ]
19- [ Route ( "/movies" , "POST,PUT,PATCH,DELETE" ) ]
20- [ Route ( "/movies/{Id}" ) ]
21- public class Movie : IReturn < MovieResponse >
22- {
23- /// <summary>
24- /// Initializes a new instance of the movie.
25- /// </summary>
26- public Movie ( )
27- {
28- this . Genres = new List < string > ( ) ;
29- }
30-
31- /// <summary>
32- /// Gets or sets the id of the movie. The id will be automatically incremented when added.
33- /// </summary>
34- [ AutoIncrement ]
35- public int Id { get ; set ; }
36-
37- public string ImdbId { get ; set ; }
38- public string Title { get ; set ; }
39- public decimal Rating { get ; set ; }
40- public string Director { get ; set ; }
41- public DateTime ReleaseDate { get ; set ; }
42- public string TagLine { get ; set ; }
43- public List < string > Genres { get ; set ; }
44- }
45-
46- /// <summary>
47- /// Define your ServiceStack web service response (i.e. Response DTO).
48- /// </summary>
49- public class MovieResponse
50- {
51- /// <summary>
52- /// Gets or sets the movie.
53- /// </summary>
54- public Movie Movie { get ; set ; }
55- }
56-
57- /// <summary>
58- /// Create your ServiceStack restful web service implementation.
59- /// </summary>
60- public class MovieService : Service
61- {
62- /// <summary>
63- /// GET /movies/{Id}
64- /// </summary>
65- public MovieResponse Get ( Movie movie )
66- {
67- return new MovieResponse
68- {
69- Movie = Db . Id < Movie > ( movie . Id ) ,
70- } ;
71- }
72-
73- /// <summary>
74- /// POST /movies
75- /// returns HTTP Response =>
76- /// 201 Created
77- /// Location: http://localhost/ServiceStack.MovieRest/movies/{newMovieId}
78- /// {newMovie DTO in [xml|json|jsv|etc]}
79- /// </summary>
80- public object Post ( Movie movie )
81- {
82- Db . Insert ( movie ) ;
83- var newMovieId = Db . GetLastInsertId ( ) ;
84-
85- var newMovie = new MovieResponse
86- {
87- Movie = Db . Id < Movie > ( newMovieId ) ,
88- } ;
89-
90- return new HttpResult ( newMovie )
91- {
92- StatusCode = HttpStatusCode . Created ,
93- Headers =
94- {
95- { HttpHeaders . Location , base . Request . AbsoluteUri . CombineWith ( newMovieId . ToString ( ) ) }
96- }
97- } ;
98- }
99-
100- /// <summary>
101- /// PUT /movies/{id}
102- /// </summary>
103- public object Put ( Movie movie )
104- {
105- Db . Update ( movie ) ;
106-
107- return new HttpResult
108- {
109- StatusCode = HttpStatusCode . NoContent ,
110- Headers =
111- {
112- { HttpHeaders . Location , this . RequestContext . AbsoluteUri . CombineWith ( movie . Id . ToString ( ) ) }
113- }
114- } ;
115- }
116-
117- /// <summary>
118- /// DELETE /movies/{Id}
119- /// </summary>
120- public object Delete ( Movie request )
121- {
122- Db . DeleteById < Movie > ( request . Id ) ;
123-
124- return new HttpResult
125- {
126- StatusCode = HttpStatusCode . NoContent ,
127- Headers =
128- {
129- { HttpHeaders . Location , this . RequestContext . AbsoluteUri . CombineWith ( request . Id . ToString ( ) ) }
130- }
131- } ;
132- }
133- }
134-
135- /// <summary>
136- /// Define your ServiceStack web service request (i.e. Request DTO).
137- /// </summary>
138- /// <remarks>The route is defined here rather than in the AppHost.</remarks>
139- [ Api ( "Find movies by genre, or all movies if no genre is provided" ) ]
140- [ Route ( "/movies" , "GET, OPTIONS" ) ]
141- [ Route ( "/movies/genres/{Genre}" ) ]
142- public class Movies
143- {
144- public string Genre { get ; set ; }
145- }
146-
147- /// <summary>
148- /// Define your ServiceStack web service response (i.e. Response DTO).
149- /// </summary>
150- public class MoviesResponse
151- {
152- /// <summary>
153- /// Gets or sets the list of movies.
154- /// </summary>
155- public List < Movie > Movies { get ; set ; }
156- }
157-
158- /// <summary>
159- /// Create your ServiceStack RESTful web service implementation.
160- /// </summary>
161- public class MoviesService : Service
162- {
163- /// <summary>
164- /// GET /movies
165- /// GET /movies/genres/{Genre}
166- /// </summary>
167- public object Get ( Movies request )
168- {
169- return new MoviesResponse
170- {
171- Movies = request . Genre . IsNullOrEmpty ( )
172- ? Db . Select < Movie > ( )
173- : Db . Select < Movie > ( "Genres LIKE {0}" , "%{0}%" . Fmt ( request . Genre ) )
174- } ;
175- }
176- }
5+ using System ;
6+ using System . Collections . Generic ;
7+ using System . Net ;
8+ using DataAnnotations ;
9+ using OrmLite ;
10+
11+ /// <summary>
12+ /// Define your ServiceStack web service request (i.e. Request DTO).
13+ /// </summary>
14+ /// <remarks>The route is defined here rather than in the AppHost.</remarks>
15+ [ Api ( "GET or DELETE a single movie by Id. Use POST to create a new Movie and PUT to update it" ) ]
16+ [ Route ( "/movies" , "POST,PUT,PATCH,DELETE" ) ]
17+ [ Route ( "/movies/{Id}" ) ]
18+ public class Movie : IReturn < MovieResponse >
19+ {
20+ /// <summary>
21+ /// Initializes a new instance of the movie.
22+ /// </summary>
23+ public Movie ( )
24+ {
25+ this . Genres = new List < string > ( ) ;
26+ }
27+
28+ /// <summary>
29+ /// Gets or sets the id of the movie. The id will be automatically incremented when added.
30+ /// </summary>
31+ [ AutoIncrement ]
32+ public int Id { get ; set ; }
33+
34+ public string ImdbId { get ; set ; }
35+ public string Title { get ; set ; }
36+ public decimal Rating { get ; set ; }
37+ public string Director { get ; set ; }
38+ public DateTime ReleaseDate { get ; set ; }
39+ public string TagLine { get ; set ; }
40+ public List < string > Genres { get ; set ; }
41+ }
42+
43+ /// <summary>
44+ /// Define your ServiceStack web service response (i.e. Response DTO).
45+ /// </summary>
46+ public class MovieResponse
47+ {
48+ /// <summary>
49+ /// Gets or sets the movie.
50+ /// </summary>
51+ public Movie Movie { get ; set ; }
52+ }
53+
54+ /// <summary>
55+ /// Define your ServiceStack web service request (i.e. Request DTO).
56+ /// </summary>
57+ /// <remarks>The route is defined here rather than in the AppHost.</remarks>
58+ [ Api ( "Find movies by genre, or all movies if no genre is provided" ) ]
59+ [ Route ( "/movies" , "GET, OPTIONS" ) ]
60+ [ Route ( "/movies/genres/{Genre}" ) ]
61+ public class Movies : IReturn < MoviesResponse >
62+ {
63+ public string Genre { get ; set ; }
64+ }
65+
66+ /// <summary>
67+ /// Define your ServiceStack web service response (i.e. Response DTO).
68+ /// </summary>
69+ [ DataContract ]
70+ public class MoviesResponse
71+ {
72+ /// <summary>
73+ /// Gets or sets the list of movies.
74+ /// </summary>
75+ [ DataMember ]
76+ public List < Movie > Movies { get ; set ; }
77+ }
78+
79+ /// <summary>
80+ /// Create your ServiceStack restful web service implementation.
81+ /// </summary>
82+ public class MovieService : Service
83+ {
84+ /// <summary>
85+ /// GET /movies
86+ /// GET /movies/genres/{Genre}
87+ /// </summary>
88+ public object Get ( Movies request )
89+ {
90+ return new MoviesResponse {
91+ Movies = request . Genre . IsNullOrEmpty ( )
92+ ? Db . Select < Movie > ( )
93+ : Db . Select < Movie > ( "Genres LIKE {0}" , "%{0}%" . Fmt ( request . Genre ) )
94+ } ;
95+ }
96+
97+ /// <summary>
98+ /// GET /movies/{Id}
99+ /// </summary>
100+ public MovieResponse Get ( Movie movie )
101+ {
102+ return new MovieResponse
103+ {
104+ Movie = Db . SingleById < Movie > ( movie . Id ) ,
105+ } ;
106+ }
107+
108+ /// <summary>
109+ /// POST /movies
110+ /// returns HTTP Response =>
111+ /// 201 Created
112+ /// Location: http://localhost/ServiceStack.MovieRest/movies/{newMovieId}
113+ /// {newMovie DTO in [xml|json|jsv|etc]}
114+ /// </summary>
115+ public object Post ( Movie movie )
116+ {
117+ Db . Save ( movie ) ;
118+ var newMovieId = movie . Id ;
119+
120+ var newMovie = new MovieResponse
121+ {
122+ Movie = Db . SingleById < Movie > ( newMovieId ) ,
123+ } ;
124+
125+ return new HttpResult ( newMovie )
126+ {
127+ StatusCode = HttpStatusCode . Created ,
128+ Headers = {
129+ { HttpHeaders . Location , base . Request . AbsoluteUri . CombineWith ( newMovieId . ToString ( ) ) }
130+ }
131+ } ;
132+ }
133+
134+ /// <summary>
135+ /// PUT /movies/{id}
136+ /// </summary>
137+ public object Put ( Movie movie )
138+ {
139+ Db . Update ( movie ) ;
140+
141+ return new HttpResult
142+ {
143+ StatusCode = HttpStatusCode . NoContent ,
144+ Headers =
145+ {
146+ { HttpHeaders . Location , this . Request . AbsoluteUri . CombineWith ( movie . Id . ToString ( ) ) }
147+ }
148+ } ;
149+ }
150+
151+ /// <summary>
152+ /// DELETE /movies/{Id}
153+ /// </summary>
154+ public object Delete ( Movie request )
155+ {
156+ Db . DeleteById < Movie > ( request . Id ) ;
157+
158+ return new HttpResult
159+ {
160+ StatusCode = HttpStatusCode . NoContent ,
161+ Headers = {
162+ { HttpHeaders . Location , this . Request . AbsoluteUri . CombineWith ( request . Id . ToString ( ) ) }
163+ }
164+ } ;
165+ }
166+ }
177167}
0 commit comments