/// <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); } }
/// <summary> /// Invokes the NewSession business operation. /// </summary> private async Task <NewSessionBusinessResponse> InvokeNewSession(IDatabaseConnection databaseConnection, SessionResource resource) { try { // Build the NewSession business request. NewSessionBusinessRequest newSessionBusinessRequest = new NewSessionBusinessRequest(); // Build the Session business request element. if (resource.Session != null) { NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = new NewSessionBusinessRequest.SessionBusinessRequestElement(); sessionBusinessRequestElement.Name = resource.Session.Name; sessionBusinessRequestElement.StartDate = resource.Session.StartDate; newSessionBusinessRequest.Session = sessionBusinessRequestElement; } // Invoke the NewSession business operation. NewSessionBusinessResponse newSessionBusinessResponse = await this.schedulingBusinessLogicComponent.NewSession(databaseConnection, newSessionBusinessRequest); // The business operation succeeded. return(newSessionBusinessResponse); } catch (NewSessionBusinessException newSessionBusinessException) { // Wrap the NewSession business exception into a service exception. ServiceException serviceException = ServiceExceptionBuilder.BuildServiceException( "SchedulingServiceComponent.SessionsController.Post()", newSessionBusinessException, newSessionBusinessException.Errors.Select(error => error.ErrorCode.ToString()).ToArray(), newSessionBusinessException.Errors.Select(error => error.ErroneousValue).ToArray()); // Throw the service exception. throw serviceException; } }
/// <summary> /// Executes the NewSession business operation. /// </summary> public async virtual Task <NewSessionBusinessResponse> NewSession(IDatabaseConnection databaseConnection, NewSessionBusinessRequest businessRequest) { // Validate the business request. this.ValidateNewSessionRequest(businessRequest); // Initialize the operation data. NewSessionOperationData operationData = new NewSessionOperationData(); // Validate the business operation. await this.ValidateNewSessionOperation(databaseConnection, businessRequest, operationData); // Generate a unique Session code. string sessionCode = this.uniqueTokenGenerator.GenerateUniqueToken(); // Create the Session data row. operationData.SessionDataRow = new SessionDataRow(); operationData.SessionDataRow.SessionCode = sessionCode; operationData.SessionDataRow.Name = businessRequest.Session.Name; operationData.SessionDataRow.StartDate = businessRequest.Session.StartDate; await this.sessionDataAccessComponent.Create(databaseConnection, operationData.SessionDataRow); // Build the business response. NewSessionBusinessResponse businessResponse = new NewSessionBusinessResponse(); // Build the Session business response element. NewSessionBusinessResponse.SessionBusinessResponseElement sessionBusinessResponseElement = new NewSessionBusinessResponse.SessionBusinessResponseElement(); sessionBusinessResponseElement.SessionCode = operationData.SessionDataRow.SessionCode; businessResponse.Session = sessionBusinessResponseElement; // Return the business response. return(businessResponse); }
public void ShouldReturnSessionResponseElement() { // Build the test harness. SchedulingBusinessLogicComponentTestHarness testHarness = new SchedulingBusinessLogicComponentTestHarness(); // Mock the generation of the unique token. testHarness.MockedUniqueTokenGenerator .Setup(mock => mock.GenerateUniqueToken()) .Returns("6dk61ufcuzp3f7vs") .Verifiable(); // Mock the creation of the Session data row. testHarness.MockedSessionDataAccessComponent .Setup(mock => mock.Create( It.IsAny <IDatabaseConnection>(), It.Is <SessionDataRow>(sessionDataRow => ( sessionDataRow.SessionCode == "6dk61ufcuzp3f7vs" && sessionDataRow.Name == "Session Alpha" && sessionDataRow.StartDate == new DateTime(2001, 1, 1) )))) .Returns(Task.FromResult <object>(null)) .Callback((IDatabaseConnection databaseConnection, SessionDataRow sessionDataRow) => { sessionDataRow.SessionID = 10001; }) .Verifiable(); // Build the NewSession business request. NewSessionBusinessRequest newSessionBusinessRequest = new NewSessionBusinessRequest(); // Build the Session business request element. NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = new NewSessionBusinessRequest.SessionBusinessRequestElement(); sessionBusinessRequestElement.Name = "Session Alpha"; sessionBusinessRequestElement.StartDate = new DateTime(2001, 1, 1); newSessionBusinessRequest.Session = sessionBusinessRequestElement; // Invoke the NewSession business operation. NewSessionBusinessResponse newSessionBusinessResponse = testHarness.SchedulingBusinessLogicComponent.NewSession(testHarness.MockedDatabaseConnection, newSessionBusinessRequest).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Validate the Session business response element. Assert.IsNotNull(newSessionBusinessResponse); Assert.IsNotNull(newSessionBusinessResponse.Session); Assert.AreEqual("6dk61ufcuzp3f7vs", newSessionBusinessResponse.Session.SessionCode); }