Search code examples
c#servicestackormlite-servicestack

Service Stack put data in variables instead of database


I use service Stack to store data in my database. With the example MovieService a simple HTTP POST allows to store data. http://mono.servicestack.net/ServiceStack.MovieRest/

I would like put some of these posted data in a variable without store it in my database. With those data I will be able to make some SQL requests to get some foreign KEYs. I would like to know if this is possible? And if yes give me a tips to do it. Thank you


Solution

  • Ok It was super easy, there is my code (copy from ServiceStack.MovieRest example):

    /// <summary>
    ///     Define your ServiceStack web service request (i.e. Request DTO).
    /// </summary>
    /// <remarks>The route is defined here rather than in the AppHost.</remarks>
    [Api("GET or DELETE a single movie by Id. Use POST to create a new LifetouchRespRate and PUT to update it")]
    [Route("/movies", "POST,PUT,PATCH,DELETE")]
    [Route("/movies/{Id}")]
    public class Movie : IReturn<MovieResponse>
    {
        /// <summary>
        ///     Initializes a new instance of the movie.
        /// </summary>
        public Movie()
        {
            this.Genres = new List<string>();
        }
    
        /// <summary>
        ///     Gets or sets the id of the movie. The id will be automatically incremented when added.
        /// </summary>
        [AutoIncrement]
        public int Id { get; set; }
    
        public string ImdbId { get; set; }
        public string Title { get; set; }
        public decimal Rating { get; set; }
        public string Director { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string TagLine { get; set; }
        public List<string> Genres { get; set; }
    }
    
    /// <summary>
    ///     Define your ServiceStack web service response (i.e. Response DTO).
    /// </summary>
    public class MovieResponse
    {
        /// <summary>
        ///     Gets or sets the movie.
        /// </summary>
        public Movie Movie { get; set; }
    }
    
    /// <summary>
    ///     Create your ServiceStack restful web service implementation.
    /// </summary>
    public class MovieService : Service
    {
        /// <summary>
        ///     GET /movies/{Id}
        /// </summary>
        public MovieResponse Get(Movie movie)
        {
            return new MovieResponse
                       {
                           Movie = Db.Id<Movie>(movie.Id),
                       };
        }
    
        /// <summary>
        ///     POST /movies
        ///     returns HTTP Response =>
        ///     201 Created
        ///     Location: http://localhost/ServiceStack.MovieRest/movies/{newMovieId}
        ///     {newMovie DTO in [xml|json|jsv|etc]}
        /// </summary>
        public object Post(Movie movie)
        {
            Db.Insert(movie);
            var newMovieId = Db.GetLastInsertId();
    
            var newMovie = new MovieResponse
                               {
                                   Movie = Db.Id<Movie>(newMovieId),
                               };
    
            return new HttpResult(newMovie)
                       {
                           StatusCode = HttpStatusCode.Created,
                           Headers =
                               {
                                   {HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newMovieId.ToString())}
                               }
                       };           
        }
    

    In the class MovieService it is the below raw to register data in the database, so just delete it to don't save in the database:

    Db.Insert(movie);
    

    So to create variable to get all information I can process like this:

            String imdbid = movie.ImdbId;
            String title = movie.Title;
            decimal rating = movie.Rating;
    

    I made this manipulation because is receive data from an other application via an http-post, I have to share all information from one post, on several tables. I use an SQL request to found my Primary key with information I get.

    Thank you for your links Scott it's help me a lot. I know it's look really easy for you but for a beginner like me it's a bit tricky and so cool when you got it :) Cheers!