コード例 #1
0
        public IHttpActionResult PostBooking(ApiBooking booking)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var persistedBooking = booking.ToDbBooking();

            persistedBooking.Heroes = booking.Heroes.Select(_ => this.db.Heroes.Find(_.Id)).Where(_ => _ != null).ToList();

            var hero = persistedBooking.Heroes.FirstOrDefault();

            if (hero != null)
            {
                persistedBooking.DebitHours         = Math.Max((persistedBooking.EndTime - persistedBooking.StartTime).TotalHours, 0);
                persistedBooking.ActualHours        = persistedBooking.DebitHours;
                persistedBooking.TotalDebitPrice    = persistedBooking.DebitHours * hero.DebitPrice;
                persistedBooking.TotalInternalPrice = persistedBooking.ActualHours * hero.InternalPrice;
            }

            this.db.Bookings.Add(persistedBooking);
            this.db.SaveChanges();

            this.Hub.Clients.All.bookingCreated(persistedBooking.ToApiBooking());
            return(this.CreatedAtRoute("DefaultApi", new { id = persistedBooking.Id }, persistedBooking.ToApiBooking()));
        }
コード例 #2
0
        public async Task <Booking> PostCreate(BookingNew booking)
        {
            var client = new Unknowntype();

            var heroes = await client.Heroes.PostHeroesWithOperationResponseAsync(new ApiHeroSearch { StartTime = booking.StartTime, EndTime = booking.EndTime, Text = booking.Description });

            var hero = heroes.Body.FirstOrDefault();

            var apiBooking = new ApiBooking
            {
                Id          = 0,
                CustomerId  = booking.UserId,
                StartTime   = booking.StartTime,
                EndTime     = booking.EndTime,
                Description = booking.Description,
                Heroes      = new List <ApiHeroBasic> {
                    new ApiHeroBasic {
                        Id = hero.Id, Name = hero.Name
                    }
                },
                Location = string.Empty,
                City     = string.Empty,
                Country  = string.Empty,
                Status   = string.Empty
            };

            try
            {
                var created = await client.Bookings.PostBookingWithOperationResponseAsync(apiBooking);
            }
            catch (Exception ex)
            {}

            return(new Booking());
        }
コード例 #3
0
        public IHttpActionResult PutBooking(int id, ApiBooking booking)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (id != booking.Id)
            {
                return(this.BadRequest());
            }

            var persistedBooking = this.db.Bookings.Find(id);

            if (persistedBooking == null)
            {
                return(this.NotFound());
            }

            persistedBooking.Status = booking.Status;
            this.db.SaveChanges();

            this.Hub.Clients.All.bookingUpdated(persistedBooking.ToApiBooking());
            return(this.StatusCode(HttpStatusCode.NoContent));
        }
コード例 #4
0
 /// <param name='operations'>
 /// Reference to the DqccomicsMobileapiService.IBookings.
 /// </param>
 /// <param name='id'>
 /// Required.
 /// </param>
 /// <param name='booking'>
 /// Required.
 /// </param>
 public static object PutBooking(this IBookings operations, int id, ApiBooking booking)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IBookings)s).PutBookingAsync(id, booking);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
コード例 #5
0
        public async Task Post([FromBody] ApiBooking booking)
        {
            var bookingId = booking.Id;
            IBookingServiceActor bookingserviceActor = GetBookingServiceActor(ref bookingId);

            booking.Id = bookingId;

            var mappedObject = AutoMapper.Mapper.Map <ApiBooking, Booking>(booking);
            await bookingserviceActor.AddBooking(mappedObject, CancellationToken.None);
        }
コード例 #6
0
        /// <param name='operations'>
        /// Reference to the DqccomicsMobileapiService.IBookings.
        /// </param>
        /// <param name='id'>
        /// Required.
        /// </param>
        /// <param name='booking'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public static async Task <object> PutBookingAsync(this IBookings operations, int id, ApiBooking booking, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            Microsoft.Rest.HttpOperationResponse <object> result = await operations.PutBookingWithOperationResponseAsync(id, booking, cancellationToken).ConfigureAwait(false);

            return(result.Body);
        }
コード例 #7
0
        /// <param name='operations'>
        /// Reference to the DqccomicsMobileapiService.IBookings.
        /// </param>
        /// <param name='booking'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public static async Task <ApiBooking> PostBookingAsync(this IBookings operations, ApiBooking booking, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            Microsoft.Rest.HttpOperationResponse <DqccomicsMobileapiService.Models.ApiBooking> result = await operations.PostBookingWithOperationResponseAsync(booking, cancellationToken).ConfigureAwait(false);

            return(result.Body);
        }
コード例 #8
0
        /// <param name='id'>
        /// Required.
        /// </param>
        /// <param name='booking'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public async Task <HttpOperationResponse <object> > PutBookingWithOperationResponseAsync(int id, ApiBooking booking, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            // Validate
            if (booking == null)
            {
                throw new ArgumentNullException("booking");
            }

            // Tracing
            bool   shouldTrace  = ServiceClientTracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("id", id);
                tracingParameters.Add("booking", booking);
                ServiceClientTracing.Enter(invocationId, this, "PutBookingAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/api/Bookings/";
            url = url + Uri.EscapeDataString(id.ToString());
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = new HttpRequestMessage();

            httpRequest.Method     = HttpMethod.Put;
            httpRequest.RequestUri = new Uri(url);

            // Set Headers

            // Set Credentials
            if (this.Client.Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }

            // Serialize Request
            string requestContent = null;
            JToken requestDoc     = booking.SerializeJson(null);

            requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
            httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
            httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            // Send Request
            if (shouldTrace)
            {
                ServiceClientTracing.SendRequest(invocationId, httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            HttpResponseMessage httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            if (shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
            }
            HttpStatusCode statusCode = httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (statusCode != HttpStatusCode.NoContent)
            {
                HttpOperationException <object> ex = new HttpOperationException <object>();
                ex.Request  = httpRequest;
                ex.Response = httpResponse;
                ex.Body     = null;
                if (shouldTrace)
                {
                    ServiceClientTracing.Error(invocationId, ex);
                }
                throw ex;
            }

            // Create Result
            HttpOperationResponse <object> result = new HttpOperationResponse <object>();

            result.Request  = httpRequest;
            result.Response = httpResponse;

            // Deserialize Response
            object resultModel = default(object);

            result.Body = resultModel;

            if (shouldTrace)
            {
                ServiceClientTracing.Exit(invocationId, result);
            }
            return(result);
        }
コード例 #9
0
        /// <param name='id'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public async Task <HttpOperationResponse <ApiBooking> > DeleteBookingWithOperationResponseAsync(int id, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            // Tracing
            bool   shouldTrace  = ServiceClientTracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("id", id);
                ServiceClientTracing.Enter(invocationId, this, "DeleteBookingAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/api/Bookings/";
            url = url + Uri.EscapeDataString(id.ToString());
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = new HttpRequestMessage();

            httpRequest.Method     = HttpMethod.Delete;
            httpRequest.RequestUri = new Uri(url);

            // Set Credentials
            if (this.Client.Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }

            // Send Request
            if (shouldTrace)
            {
                ServiceClientTracing.SendRequest(invocationId, httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            HttpResponseMessage httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            if (shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
            }
            HttpStatusCode statusCode = httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (statusCode != HttpStatusCode.OK)
            {
                HttpOperationException <object> ex = new HttpOperationException <object>();
                ex.Request  = httpRequest;
                ex.Response = httpResponse;
                ex.Body     = null;
                if (shouldTrace)
                {
                    ServiceClientTracing.Error(invocationId, ex);
                }
                throw ex;
            }

            // Create Result
            HttpOperationResponse <ApiBooking> result = new HttpOperationResponse <ApiBooking>();

            result.Request  = httpRequest;
            result.Response = httpResponse;

            // Deserialize Response
            if (statusCode == HttpStatusCode.OK)
            {
                ApiBooking resultModel = new ApiBooking();
                JToken     responseDoc = null;
                if (string.IsNullOrEmpty(responseContent) == false)
                {
                    responseDoc = JToken.Parse(responseContent);
                }
                if (responseDoc != null)
                {
                    resultModel.DeserializeJson(responseDoc);
                }
                result.Body = resultModel;
            }

            if (shouldTrace)
            {
                ServiceClientTracing.Exit(invocationId, result);
            }
            return(result);
        }