Example #1
0
        /// <summary>
        /// Executes the HTTP GET method.
        /// </summary>
        public async Task <HttpResponseMessage> Get()
        {
            // Open a database connection and begin a database transaction.
            using (IDatabaseConnection databaseConnection = await this.databaseConnectionProvider.OpenDatabaseConnection())
                using (IDatabaseTransaction databaseTransaction = databaseConnection.BeginDatabaseTransaction())
                {
                    // Invoke the GetSessions business operation.
                    GetSessionsBusinessResponse getSessionsBusinessResponse = await this.InvokeGetSessions(databaseConnection);

                    // Build the Session resources.
                    List <SessionResource> sessionResources = new List <SessionResource>();
                    foreach (GetSessionsBusinessResponse.SessionBusinessResponseElement sessionBusinessResponseElement in getSessionsBusinessResponse.Sessions)
                    {
                        // Build the Session resource.
                        SessionResource sessionResource = new SessionResource();
                        sessionResources.Add(sessionResource);

                        // Build the Session resource element.
                        SessionResource.SessionResourceElement sessionResourceElement = new SessionResource.SessionResourceElement();
                        sessionResourceElement.SessionCode = sessionBusinessResponseElement.SessionCode;
                        sessionResourceElement.Name        = sessionBusinessResponseElement.Name;
                        sessionResourceElement.StartDate   = sessionBusinessResponseElement.StartDate;
                        sessionResource.Session            = sessionResourceElement;
                    }

                    // Commit the database transaction.
                    databaseTransaction.Commit();

                    // Return an HTTP response message.
                    HttpResponseMessage httpResponseMessage = this.Request.CreateResponse(HttpStatusCode.OK, sessionResources.ToArray());
                    return(httpResponseMessage);
                }
        }
Example #2
0
        /// <summary>
        /// Executes the HTTP POST method.
        /// </summary>
        public async Task <HttpResponseMessage> Post(SessionResource resource)
        {
            // Make sure the resource is valid.
            bool resourceIsNull    = resource == null;
            bool resourceIsInvalid = this.ModelState.IsValid == false;

            if (resourceIsNull || resourceIsInvalid)
            {
                HttpResponseMessage   httpResponseMessage   = new HttpResponseMessage(HttpStatusCode.BadRequest);
                HttpResponseException httpResponseException = new HttpResponseException(httpResponseMessage);
                throw httpResponseException;
            }

            // Open a database connection and begin a database transaction.
            using (IDatabaseConnection databaseConnection = await this.databaseConnectionProvider.OpenDatabaseConnection())
                using (IDatabaseTransaction databaseTransaction = databaseConnection.BeginDatabaseTransaction())
                {
                    // Invoke the NewSession business operation.
                    NewSessionBusinessResponse newSessionBusinessResponse = await this.InvokeNewSession(databaseConnection, resource);

                    // Update the Session resource element.
                    if (resource.Session != null)
                    {
                        resource.Session.SessionCode = newSessionBusinessResponse.Session.SessionCode;
                    }

                    // Commit the database transaction.
                    databaseTransaction.Commit();

                    // Build an HTTP response message.
                    HttpResponseMessage httpResponseMessage = this.Request.CreateResponse(HttpStatusCode.Created, resource);
                    return(httpResponseMessage);
                }
        }