/// <summary> /// Wysyła zapytanie dodające usera do bazy /// </summary> /// <param name="offerInfo"></param> /// <param name="vendor"></param> /// <param name="place"></param> /// <param name="room"></param> /// <returns></returns> public async Task SaveOfferAsync(OfferInfo offerInfo, User vendor, Place place, Room room) { OfferAllDataDto dto = new OfferAllDataDto() { Vendor = vendor, OfferInfo = offerInfo, Place = place, Room = room }; await Post <OfferAllDataDto, object>("saveOffer", dto); }
public IHttpActionResult SaveOfferAsync(OfferAllDataDto dto) { using (var context = _provider.GetNewContext()) { using (var transaction = new TransactionScope()) { Offer offerToAdd = new Offer(); User user = context.Users.FirstOrDefault(x => x.Id == dto.Vendor.Id); if (user == null) { return(NotFound()); } offerToAdd.Vendor = user; offerToAdd.OfferInfo = dto.OfferInfo; Place place = context.Places.FirstOrDefault(p => p.PlaceName.Equals(dto.Place.PlaceName) && p.Address.City.Equals(dto.Place.Address.City) && p.Address.Street.Equals(dto.Place.Address.Street) && p.Address.LocalNumber == dto.Place.Address.LocalNumber); if (place != null) { //istnieje to miejsce w bazie danych Room room = context.Rooms.FirstOrDefault(r => r.PlaceId == place.Id && r.Number == dto.Room.Number); if (room != null) { //istnieje oferta na ten pokój List <Offer> off = context.Offers.Where(offer => offer.RoomId == room.Id) .Include(o => o.OfferInfo) .ToList(); if (off.Any(offer => (offer.OfferInfo.OfferStartTime <= dto.OfferInfo.OfferStartTime && offer.OfferInfo.OfferEndTime >= dto.OfferInfo.OfferStartTime) || (offer.OfferInfo.OfferStartTime >= dto.OfferInfo.OfferStartTime && offer.OfferInfo.OfferEndTime <= dto.OfferInfo.OfferEndTime))) { return(BadRequest()); } //żadna oferta nie koliduje offerToAdd.Room = room; } else { //nowy pokój offerToAdd.Room = dto.Room; offerToAdd.Room.Place = place; } } else { offerToAdd.Room = dto.Room; offerToAdd.Room.Place = dto.Place; offerToAdd.Room.Place.Address = dto.Place.Address; } user.MyOffers.Add(offerToAdd); //zapisanie do historii HistoricalOffer historicalOffer = new HistoricalOffer(); historicalOffer.OfferInfo = offerToAdd.OfferInfo; historicalOffer.Vendor = offerToAdd.Vendor; historicalOffer.Room = offerToAdd.Room; historicalOffer.Room.Place = offerToAdd.Room.Place; historicalOffer.Room.Place.Address = offerToAdd.Room.Place.Address; historicalOffer.OriginalOffer = offerToAdd; user.MyHistoricalOffers.Add(historicalOffer); //ewentualna zmiana rangi int c = user.MyHistoricalOffers.Count; if (c >= 4 && c < 8) { user.Rank = context.Ranks.FirstOrDefault(r => r.Name.Equals("Junior")); } else if (c >= 8 && c < 15) { user.Rank = context.Ranks.FirstOrDefault(r => r.Name.Equals("Znawca")); } else if (c >= 15 && c < 25) { user.Rank = context.Ranks.FirstOrDefault(r => r.Name.Equals("Mistrz")); } else if (c >= 25) { user.Rank = context.Ranks.FirstOrDefault(r => r.Name.Equals("Guru")); } context.SaveChanges(); transaction.Complete(); } } return(Ok(true)); }