public IHttpActionResult PutTraveler(int id, [FromBody] TravelerModel traveler) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != traveler.Id) { return(BadRequest()); } //Check for promo code if (!String.IsNullOrEmpty(traveler.PromoCode)) { List <PromoCode> promoCodes = Uow.Repository <PromoCode>().Query().Get().Where(t => t.Code == traveler.PromoCode).ToList(); if (promoCodes.Count <= 0) { return(BadRequest("Invalid Promo Code")); } } Traveler travelerEntity = traveler.ToTraveler(); //Set modified date to now. travelerEntity.ModifiedDate = DateTime.UtcNow; Uow.Repository <Traveler>().Update(travelerEntity); try { Uow.Save(); TravelerModel tm = new TravelerModel(travelerEntity); return(Ok(tm)); } catch (DbUpdateConcurrencyException) { if (!TravelerExists(id)) { return(NotFound()); } else { throw; } } }
public IHttpActionResult PostTraveler(TravelerModel traveler) { try{ if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Check for promo code if (!String.IsNullOrEmpty(traveler.PromoCode)) { List <PromoCode> promoCodes = Uow.Repository <PromoCode>().Query().Get().Where(t => t.Code == traveler.PromoCode).ToList(); if (promoCodes.Count <= 0) { return(BadRequest("Invalid Promo Code")); } } Traveler travelerEntity = traveler.ToTraveler(); //Set creation and modified date to now. travelerEntity.CreatedDate = DateTime.UtcNow; travelerEntity.ModifiedDate = DateTime.UtcNow; Uow.Repository <Traveler>().Insert(travelerEntity); Uow.Save(); TravelerModel tm = new TravelerModel(travelerEntity); return(CreatedAtRoute("DefaultApi", new { id = tm.Id }, tm)); } catch (Exception ex) { string msg = RecordException(ex, "TravelerController.PostTraveler"); HttpResponseMessage responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, msg); throw new HttpResponseException(responseMessage); } }