public Hotels EditHotel(string userId, IncomingEditHotel hotelToEdit) { var hotel = this.dbContext.Hotels.FirstOrDefault(x => x.Id == hotelToEdit.Id); if (hotel == null) { throw new Exception("Hotel does not exist."); } var auth = this.CanUserEditHotelSiteAdmin(userId, hotel.Id); if (auth == false) { throw new UnauthorizedAccessException(); } hotel.Name = hotelToEdit.Name; hotel.Address = hotelToEdit.Address; hotel.TaxRate = hotelToEdit.TaxRate; hotel.Latitude = hotelToEdit.Latitude; hotel.Longitude = hotelToEdit.Longitude; hotel.IsHidden = hotelToEdit.IsHidden; return(hotel); }
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)); }