///<summary> /// Get all sightings. ///</summary> public List <SightingData> getSightings() { List <Sighting> sightings = new List <Sighting>(); List <SightingData> retList = new List <SightingData>(); try { sightings = dao.getSightings(); // update birdnames to data List <Bird> birds = dao.getBirds(); foreach (Bird bird in birds) { SightingData tmp; foreach (Sighting xx in sightings.Where(s => s.BirdID == bird.BirdID).ToList()) { tmp = new SightingData() { BirdName = bird.Name, tmpDate = xx.SightingDate, SightingID = xx.SightingID }; retList.Add(tmp); } } } catch (Exception ex) { log.Error("Error when getting sightingss", ex); throw new ApplicationException("Error when getting sightings", ex); } return(retList.OrderBy(tipu => tipu.DateFormatted).ToList()); }
public async Task <IHttpActionResult> PostMBL_SightDetails(SightingData postData) { try { //This is currently redundant, as user cannot enter a location on UI. They have to manually select one from the drop-down. int locationID; if (!int.TryParse(postData.locationSelected.ToString(), out locationID)) { //it's a new location, add it to location lookup LocationLookup locationEntity = new LocationLookup() { Name = postData.locationSelected.ToString(), GridReference = postData.gridReference, boolManuallyAddedRecord = true, boolApprovedByAdmin = false }; db.LocationLookups.Add(locationEntity); db.SaveChanges(); //get new location ID locationID = locationEntity.LocationID; } //convert date time from web page into SQL format DateTime tempSightDate = Convert.ToDateTime(postData.dt); DateTime tempSightTime = Convert.ToDateTime(postData.myTime); DateTime sightDate = tempSightDate.Date; TimeSpan sightTime = tempSightTime.TimeOfDay; DateTime sightDateTime = sightDate.Date.Add(sightTime); var newSightDetails = new MBL_SightDetails { SightReportUserID = postData.sightReportUserID, LocationID = locationID, GridReference = postData.gridReference, SightDateTime = sightDateTime, SubmitDateTime = DateTime.Now }; db.MBL_SightDetails.Add(newSightDetails); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = newSightDetails.SightDetailsID }, newSightDetails)); } catch (Exception ex) { var error = ex.InnerException.Message; return(BadRequest("Error saving Sight details - " + error)); } }