private OfferUpdateError ValidateOfferUpdateInfo(Offer currentOffer, OfferUpdateInfo offerUpdateInfo) { var offerUpdateError = new OfferUpdateError(); if (offerUpdateInfo == null) { offerUpdateError.Id = new List <string> { "Offer update data are invalid." }; offerUpdateError.HasError = true; } // Client does not send always the id //else //{ //if (currentOffer.Id != offerUpdateInfo.Id) //{ // offerUpdateError.Id = new List<string> // { // "Offer id in the url query and post object are not equivalent." // }; // offerUpdateError.HasError = true; //} //} return(offerUpdateError); }
public async Task <IActionResult> UpdateOffer(int offerId, [FromBody] OfferUpdateInfo offerUpdateInfo) { IActionResult response = BadRequest(); try { var offer = await _database.Offer .SingleOrDefaultAsync(o => o.Id == offerId); if (offer != null) { if (offer.Landlord != HttpContext.User.GetUserId()) { response = Unauthorized(); } else { var offerUpdateInfoError = ValidateOfferUpdateInfo(offer, offerUpdateInfo); if (offerUpdateInfoError.HasError) { response = BadRequest(offerUpdateInfoError); } else { await UpdateOfferProperties(offer, offerUpdateInfo); await UpdateOffer(offer); await CreateOffereRelatedTags(offer, offerUpdateInfo); response = Ok(); } } } else { response = NotFound(); } } catch (Exception ex) { _logger.LogDebug(null, ex, "Unexpected Issue."); response = StatusCode((int)HttpStatusCode.InternalServerError); } return(response); }
private async Task CreateOffereRelatedTags(Offer currentOffer, OfferUpdateInfo offerUpdateInfo) { var addedTag = false; foreach (var tag in offerUpdateInfo.Tags) { if (!await _database.Tag.AnyAsync(t => (t.OfferId == currentOffer.Id) && (t.Title == tag))) { await _database.Tag.AddAsync(new Tag { OfferId = currentOffer.Id, Title = tag }); addedTag = true; } } if (addedTag) { await _database.SaveChangesAsync(); } }
private async Task UpdateOfferProperties(Offer currentOffer, OfferUpdateInfo offerUpdateInfo) { if ((offerUpdateInfo.Accessability != null) || (currentOffer.Accessability == null)) { currentOffer.Accessability = offerUpdateInfo.Accessability == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.BathroomDescription)) { currentOffer.BathroomDescription = offerUpdateInfo.BathroomDescription; } if ((offerUpdateInfo.BathroomNumber != null) || (currentOffer.BathroomNumber == null)) { currentOffer.BathroomNumber = offerUpdateInfo.BathroomNumber ?? 0; } if ((offerUpdateInfo.Cellar != null) || (currentOffer.Cellar == null)) { currentOffer.Cellar = offerUpdateInfo.Cellar == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.City)) { currentOffer.City = offerUpdateInfo.City; } if ((offerUpdateInfo.Commission != null) || (currentOffer.Commission == null)) { currentOffer.Commission = offerUpdateInfo.Commission ?? 0; } if ((offerUpdateInfo.Deposit != null) || (currentOffer.Deposit == null)) { currentOffer.Deposit = offerUpdateInfo.Deposit ?? 0; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.Description)) { currentOffer.Description = offerUpdateInfo.Description; } if ((offerUpdateInfo.Dryer != null) || (currentOffer.Dryer == null)) { currentOffer.Dryer = offerUpdateInfo.Dryer == true; } if ((offerUpdateInfo.Elevator != null) || (currentOffer.Elevator == null)) { currentOffer.Elevator = offerUpdateInfo.Elevator == true; } if ((offerUpdateInfo.Floor != null) || (currentOffer.Floor == null)) { currentOffer.Floor = offerUpdateInfo.Floor ?? 0; } if ((offerUpdateInfo.Furnished != null) || (currentOffer.Furnished == null)) { currentOffer.Furnished = offerUpdateInfo.Furnished == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.HeatingDescription)) { currentOffer.HeatingDescription = offerUpdateInfo.HeatingDescription; } if (offerUpdateInfo.HouseNumber != null) { currentOffer.HouseNumber = offerUpdateInfo.HouseNumber ?? 0; } if ((offerUpdateInfo.InternetSpeed != null) || (currentOffer.InternetSpeed == null)) { currentOffer.InternetSpeed = offerUpdateInfo.InternetSpeed ?? 0; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.KitchenDescription)) { currentOffer.KitchenDescription = offerUpdateInfo.KitchenDescription; } if ((offerUpdateInfo.Lan != null) || (currentOffer.Lan == null)) { currentOffer.Lan = offerUpdateInfo.Lan == true; } currentOffer.LastModified = DateTime.Now; if (!string.IsNullOrWhiteSpace(offerUpdateInfo.OfferType)) { currentOffer.OfferType = offerUpdateInfo.OfferType; } if ((offerUpdateInfo.Parking != null) || (currentOffer.Parking == null)) { currentOffer.Parking = offerUpdateInfo.Parking == true; } if ((offerUpdateInfo.Pets != null) || (currentOffer.Pets == null)) { currentOffer.Pets = offerUpdateInfo.Pets == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.PriceType)) { currentOffer.PriceType = offerUpdateInfo.PriceType; } if ((offerUpdateInfo.Rent != null) || (currentOffer.Rent == null)) { currentOffer.Rent = offerUpdateInfo.Rent ?? 0; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.RentType)) { currentOffer.RentType = offerUpdateInfo.RentType; } if ((offerUpdateInfo.Rooms != null) || (currentOffer.Rooms == null)) { currentOffer.Rooms = offerUpdateInfo.Rooms ?? 0; } if ((offerUpdateInfo.SideCosts != null) || (currentOffer.SideCosts == null)) { currentOffer.SideCosts = offerUpdateInfo.SideCosts ?? 0; } if ((offerUpdateInfo.Size != null) || (currentOffer.Size == null)) { currentOffer.Size = offerUpdateInfo.Size ?? 0; } if ((offerUpdateInfo.Status != null) || (currentOffer.Status == null)) { currentOffer.Status = offerUpdateInfo.Status ?? 0; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.Street)) { currentOffer.Street = offerUpdateInfo.Street; } if ((offerUpdateInfo.Telephone != null) || (currentOffer.Telephone == null)) { currentOffer.Telephone = offerUpdateInfo.Telephone == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.Television)) { currentOffer.Television = offerUpdateInfo.Television; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.Title)) { currentOffer.Title = offerUpdateInfo.Title; } if ((offerUpdateInfo.WashingMachine != null) || (currentOffer.WashingMachine == null)) { currentOffer.WashingMachine = offerUpdateInfo.WashingMachine == true; } if ((offerUpdateInfo.Wlan != null) || (currentOffer.Wlan == null)) { currentOffer.Wlan = offerUpdateInfo.Wlan == true; } if (!string.IsNullOrWhiteSpace(offerUpdateInfo.ZipCode)) { currentOffer.ZipCode = offerUpdateInfo.ZipCode; } if ((offerUpdateInfo.Status != null) && ((int[])Enum.GetValues(typeof(GlobalConstants.OfferStatus))).Any(i => i == offerUpdateInfo.Status)) { currentOffer.Status = offerUpdateInfo.Status; } if ((offerUpdateInfo.FullPrice != null) || (currentOffer.FullPrice == null)) { var fullPrice = 0; if (offerUpdateInfo.Rent != null) { fullPrice += (int)offerUpdateInfo.Rent; } if (offerUpdateInfo.SideCosts != null) { fullPrice += (int)offerUpdateInfo.SideCosts; } currentOffer.FullPrice = fullPrice; } if (!string.IsNullOrWhiteSpace(currentOffer.Street) && (currentOffer.HouseNumber != null) && !string.IsNullOrWhiteSpace(currentOffer.ZipCode) && !string.IsNullOrWhiteSpace(currentOffer.City) && (currentOffer.UniDistance == null)) { try { var coordinate = await GetOfferGeoCoordinates(currentOffer); if (coordinate != null) { currentOffer.Latitude = coordinate.Latitude; currentOffer.Longitude = coordinate.Longitude; currentOffer.UniDistance = Math.Round(coordinate.GetDistanceTo(_appSettings.HsFuldaCoordinate) / 1000 * 100) / 100; } } catch (Exception ex) { _logger.LogError("Error while requesting offer geo coordinate", ex); } } }