public IHttpActionResult PutCruise(int id, CruiseDto cruiseDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var cruise = _context.Cruises.SingleOrDefault(c => c.Id == id); //_context.Cruises.Remove(cruise); if (cruiseDto.ArrivalDateDto != null) { cruise.ArrivalDate = new DateTime(cruiseDto.ArrivalDateDto.Year, cruiseDto.ArrivalDateDto.Month, cruiseDto.ArrivalDateDto.Day, cruiseDto.ArrivalDateDto.Hour, cruiseDto.ArrivalDateDto.Minute, 0); } if (cruiseDto.DepartureDateDto != null) { cruise.DepartureDate = new DateTime(cruiseDto.DepartureDateDto.Year, cruiseDto.DepartureDateDto.Month, cruiseDto.DepartureDateDto.Day, cruiseDto.DepartureDateDto.Hour, cruiseDto.DepartureDateDto.Minute, 0); } if (cruiseDto.ArrivalHarborName != null) { cruise.ArrivalHarborId = _context.Harbors.SingleOrDefault(h => h.City == cruiseDto.ArrivalHarborName).Id; } if (cruiseDto.DepartureHarborName != null) { cruise.DepartureHarborId = _context.Harbors.SingleOrDefault(h => h.City == cruiseDto.DepartureHarborName).Id; } if (cruiseDto.ShipName != null) { cruise.ShipId = _context.Ships.SingleOrDefault(s => s.Name == cruiseDto.ShipName).Id; } //_context.Cruises.Add(cruise); _context.SaveChanges(); return(Ok()); }
public IHttpActionResult PostCruise(CruiseDto cruiseDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var cruise = new Cruise(); cruise.ArrivalDate = new DateTime(cruiseDto.ArrivalDateDto.Year, cruiseDto.ArrivalDateDto.Month, cruiseDto.ArrivalDateDto.Day, cruiseDto.ArrivalDateDto.Hour, cruiseDto.ArrivalDateDto.Minute, 0); cruise.DepartureDate = new DateTime(cruiseDto.DepartureDateDto.Year, cruiseDto.DepartureDateDto.Month, cruiseDto.DepartureDateDto.Day, cruiseDto.DepartureDateDto.Hour, cruiseDto.DepartureDateDto.Minute, 0); cruise.ArrivalHarborId = _context.Harbors.SingleOrDefault(h => h.City == cruiseDto.ArrivalHarborName).Id; cruise.DepartureHarborId = _context.Harbors.SingleOrDefault(h => h.City == cruiseDto.DepartureHarborName).Id; cruise.ShipId = _context.Ships.SingleOrDefault(s => s.Name == cruiseDto.ShipName).Id; _context.Cruises.Add(cruise); _context.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = cruise.Id }, cruise)); }
public IEnumerable <CruiseDto> GetCruises() { var cruises = new List <Cruise>(); cruises = _context.Cruises.ToList(); var cruisesResponse = new List <CruiseDto>(); foreach (Cruise cruise in cruises) { var cruiseDto = new CruiseDto(); cruiseDto.ArrivalDate = cruise.ArrivalDate; cruiseDto.DepartureDate = cruise.DepartureDate; cruiseDto.ShipName = _context.Ships.SingleOrDefault(s => s.Id == cruise.ShipId).Name; cruiseDto.DepartureHarborName = _context.Harbors.SingleOrDefault(h => h.Id == cruise.DepartureHarborId).City; cruiseDto.ArrivalHarborName = _context.Harbors.SingleOrDefault(h => h.Id == cruise.ArrivalHarborId).City; cruiseDto.Id = cruise.Id; cruiseDto.OwnerId = _context.Ships.SingleOrDefault(s => s.Id == cruise.ShipId).OwnerId; cruisesResponse.Add(cruiseDto); } return(cruisesResponse); }