Example #1
0
        public HttpResponseMessage Hotel(IncomingBoundingBoxLocation location)
        {
            return(ErrorFactory.Handle(() =>
            {
                var currentUserId = User?.Identity?.GetUserId();

                if (string.IsNullOrWhiteSpace(currentUserId))
                {
                    throw new UnauthorizedAccessException();
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    var theLocation = LocationBounded.FromDegrees(location.Latitude, location.Longitude);

                    var boundingBox = theLocation.BoundingCoordinates(location.Distance);

                    if (boundingBox.Length != 2)
                    {
                        throw new Exception("The bounding box is not in the correct format.");
                    }

                    var bottomLeft = boundingBox[0].ConvertToLatLonLocation();
                    var topRight = boundingBox[1].ConvertToLatLonLocation();

                    var hotels = unitOfWork.Hotels.GetHotelsInRadius(currentUserId, bottomLeft, topRight, location.Distance).ToList();

                    var outgoingHotels = hotels.Select(x => OutgoingHotel.Parse(x)).ToList();

                    return JsonFactory.CreateJsonMessage(outgoingHotels, HttpStatusCode.OK, this.Request);
                }
            }, this.Request));
        }
Example #2
0
        public HttpResponseMessage HotelsByUserId(string userId)
        {
            return(ErrorFactory.Handle(() =>
            {
                var currentUserId = User?.Identity?.GetUserId();

                if (string.IsNullOrWhiteSpace(currentUserId))
                {
                    throw new Exception();
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    var hotels = unitOfWork.Hotels.GetHotelsByUserId(userId).ToList();

                    var outgoingHotels = hotels.Select(x => OutgoingHotel.Parse(x)).ToList();

                    return JsonFactory.CreateJsonMessage(outgoingHotels, HttpStatusCode.OK, this.Request);
                }
            }, this.Request));
        }
Example #3
0
        public async Task <HttpResponseMessage> EditHotel(IncomingEditHotel hotel)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                var userId = User?.Identity?.GetUserId();

                if (userId == null)
                {
                    throw new Exception("User not found.");
                }

                if (hotel == null)
                {
                    throw new InvalidModelException("hotel cannot be null");
                }

                if (hotel.TaxRate < 0 || hotel.TaxRate > 1)
                {
                    throw new InvalidModelException("Tax rate must be greater than 0 and less than 1");
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    var finHotel = unitOfWork.Hotels.EditHotel(userId, hotel);

                    unitOfWork.Complete();

                    try
                    {
                        if (hotel.Image != null && !string.IsNullOrWhiteSpace(hotel.Image.Data))
                        {
                            //This is added to help yogita with her base64 problems.
                            hotel.Image.Data = hotel.Image.Data.Replace(' ', '+');


                            var data = ImageFactory.ConvertBase64ToArray(hotel.Image.Data);

                            GalleryManager galMan = new GalleryManager();

                            var pictureId = await galMan.UploadImage(data, userId);

                            if (pictureId == null)
                            {
                                throw new Exception();
                            }

                            var tempHotel = unitOfWork.Hotels.SetHotelImageADMIN(userId, finHotel.Id, pictureId ?? Guid.NewGuid());

                            unitOfWork.Complete();
                            finHotel = tempHotel;
                        }
                    }
                    catch (Exception)
                    {
                        //Maybe try to delete image.
                    }

                    return JsonFactory.CreateJsonMessage(OutgoingHotel.Parse(finHotel), HttpStatusCode.OK, this.Request);
                }
            }, this.Request));
        }