public ActionResult Update(DogEventViewModel model) { if (!LoginHelper.IsAuthenticated()) { return(RedirectToAction("Login", "Account", new { returnUrl = this.Request.Url.AbsoluteUri })); } var lostPersonTrackPoints = new Trkseg { Trkpt = model.LostPersonTrackPoints }; var dogTrackPoints = new Trkseg { Trkpt = model.DogTrackPoints }; UpdateTracks(model.DogId, model.EventId, model.LostPersonTrackFilename, model.DogTrackFilename, lostPersonTrackPoints, dogTrackPoints); var dogEventModel = new DogEventModel { LostPerson = model.LostPerson, Notes = model.Notes, Weather = model.Weather, DogId = model.DogId, EventId = model.EventId }; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LoginHelper.GetToken()); System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, client.BaseAddress + $"dogevents/dogevent?dogId={model.DogId}&eventId={model.EventId}"); var dogEventSerialized = JsonConvert.SerializeObject(dogEventModel); message.Content = new StringContent(dogEventSerialized, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage responseMessage = client.SendAsync(message).Result; if (responseMessage.IsSuccessStatusCode) //200 OK { message.Dispose(); var responseData = responseMessage.Content.ReadAsStringAsync().Result; var definition = new { DogId = "", EventId = "" }; var ids = JsonConvert.DeserializeAnonymousType(responseData, definition); return(RedirectToAction("DogEvent", new { dogId = ids.DogId, eventId = ids.EventId })); } else // msg why not ok { ViewBag.Message = "code: " + responseMessage.StatusCode; return(View("Error")); } }
public List <DogEventModel> Get() { var dogEvents = unitOfWork.DogEventRepository.GetAll().ToList(); var dogEventModels = new List <DogEventModel>(); foreach (var dogEvent in dogEvents) { var dogEventModel = new DogEventModel { DogId = dogEvent.DogId, Dog = new DogModel { DogId = dogEvent.Dog.DogId, Breed = dogEvent.Dog.Breed, Name = dogEvent.Dog.Name, GuideIdAndName = new IdNameModel { Id = dogEvent.Dog.Guide.GuideId, Name = $"{dogEvent.Dog.Guide.FirstName} {dogEvent.Dog.Guide.LastName}" } }, EventId = dogEvent.EventId, Event = new EventModel { City = dogEvent.Event.City, Date = dogEvent.Event.Date, Description = dogEvent.Event.Description, EventId = dogEvent.Event.EventId, Notes = dogEvent.Event.Notes, StreetOrLocation = dogEvent.Event.StreetOrLocation, Title = dogEvent.Event.Title }, DogTrackBlobUrl = dogEvent.DogTrackBlobUrl, LostPerson = dogEvent.LostPerson, LostPersonTrackBlobUrl = dogEvent.LostPersonTrackBlobUrl, Notes = dogEvent.Notes, Weather = dogEvent.Weather }; dogEventModels.Add(dogEventModel); } return(dogEventModels); }
public IActionResult Add([FromBody] DogEventModel obj) { if (!IsCurrentUserAdmin() && !IsCurrentUserMember()) { return(Forbid()); } //dogs will be added to the event later var dogEvent = new DogEvent { DogId = obj.DogId, EventId = obj.EventId, DogTrackBlobUrl = obj.DogTrackBlobUrl, LostPerson = obj.LostPerson, LostPersonTrackBlobUrl = obj.LostPersonTrackBlobUrl, Notes = obj.Notes, Weather = obj.Weather }; unitOfWork.DogEventRepository.Insert(dogEvent); unitOfWork.Commit(); //var accessToken = HttpContext.GetTokenAsync("access_token").Result; return(new ObjectResult(new { eventId = dogEvent.EventId, dogId = dogEvent.DogId })); }
public DogEventModel Get(int dogId, int eventId) { //return unitOfWork.GuideRepository.GetById(id); var dogEvent = unitOfWork.DogEventRepository.GetByIds(dogId, eventId); var DogEventModel = new DogEventModel() { DogId = dogEvent.DogId, Dog = new DogModel { DogId = dogEvent.Dog.DogId, Breed = dogEvent.Dog.Breed, Name = dogEvent.Dog.Name, GuideIdAndName = new IdNameModel { Id = dogEvent.Dog.Guide.GuideId, Name = $"{dogEvent.Dog.Guide.FirstName} {dogEvent.Dog.Guide.LastName}" } }, EventId = dogEvent.EventId, Event = new EventModel { City = dogEvent.Event.City, Date = dogEvent.Event.Date, Description = dogEvent.Event.Description, EventId = dogEvent.Event.EventId, Notes = dogEvent.Event.Notes, StreetOrLocation = dogEvent.Event.StreetOrLocation, Title = dogEvent.Event.Title }, DogTrackBlobUrl = dogEvent.DogTrackBlobUrl, LostPerson = dogEvent.LostPerson, LostPersonTrackBlobUrl = dogEvent.LostPersonTrackBlobUrl, Notes = dogEvent.Notes, Weather = dogEvent.Weather, }; return(DogEventModel); }
public ActionResult AddDogEvent(DogEventModel model, HttpPostedFileBase lostPersonTrackFile, HttpPostedFileBase dogTrackFile) { if (!LoginHelper.IsAuthenticated()) { return(RedirectToAction("Login", "Account", new { returnUrl = this.Request.Url.AbsoluteUri })); } else if (!LoginHelper.IsCurrentUserAdmin() && !LoginHelper.IsCurrentUserMember()) { return(RedirectToAction("Error", "Home", new { error = "Nie masz wystarczających uprawnień by dodać wydarzenie psa." })); } if (lostPersonTrackFile != null && dogTrackFile != null) { var originalLostPersonTrackStream = lostPersonTrackFile.InputStream; var cleanedLostPersonTrackStream = new MemoryStream(); using (var reader = new StreamReader(originalLostPersonTrackStream)) { XDocument gpxDoc = XDocument.Load(reader); var originalFileSerializer = new XmlSerializer(typeof(Gpx)); var gpx = (Gpx)originalFileSerializer.Deserialize(gpxDoc.Root.CreateReader()); var trkSeg = gpx.Trk.Trkseg; var cleanedFileSerializer = new XmlSerializer(typeof(Trkseg)); cleanedFileSerializer.Serialize(cleanedLostPersonTrackStream, trkSeg); cleanedLostPersonTrackStream.Position = 0; } var originalDogTrackStream = dogTrackFile.InputStream; var cleanedDogTrackStream = new MemoryStream(); using (var reader = new StreamReader(originalDogTrackStream)) { XDocument gpxDoc = XDocument.Load(reader); var originalFileSerializer = new XmlSerializer(typeof(Gpx)); var gpx = (Gpx)originalFileSerializer.Deserialize(gpxDoc.Root.CreateReader()); var trkSeg = gpx.Trk.Trkseg; var cleanedFileSerializer = new XmlSerializer(typeof(Trkseg)); cleanedFileSerializer.Serialize(cleanedDogTrackStream, trkSeg); cleanedDogTrackStream.Position = 0; } MultipartFormDataContent form = new MultipartFormDataContent(); var lostPersonStreamContent = new StreamContent(cleanedLostPersonTrackStream); var lostPersonImageContent = new ByteArrayContent(lostPersonStreamContent.ReadAsByteArrayAsync().Result); lostPersonImageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); var lostPersonFileName = lostPersonTrackFile.FileName + Guid.NewGuid().ToString(); form.Add(lostPersonImageContent, lostPersonFileName, Path.GetFileName(lostPersonFileName)); var dogStreamContent = new StreamContent(cleanedDogTrackStream); var dogImageContent = new ByteArrayContent(dogStreamContent.ReadAsByteArrayAsync().Result); dogImageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); var dogFileName = dogTrackFile.FileName + Guid.NewGuid().ToString(); form.Add(dogImageContent, dogFileName, Path.GetFileName(dogFileName)); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LoginHelper.GetToken()); var response = client.PostAsync("DogEvents/Upload", form).Result; if (response.IsSuccessStatusCode) { //get blob urls - is it that simple or it has to be returned? var lostPersonTrackBlobUrl = @"https://kgtstorage.blob.core.windows.net/tracks/" + lostPersonFileName; var dogTrackBlobUrl = @"https://kgtstorage.blob.core.windows.net/tracks/" + dogFileName; //add blob urls to model model.LostPersonTrackBlobUrl = lostPersonTrackBlobUrl; model.DogTrackBlobUrl = dogTrackBlobUrl; } else { ViewBag.Message = "Upload failed. Reason: " + response.StatusCode; return(View("Error")); } } //add dogevent client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LoginHelper.GetToken()); HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, client.BaseAddress + "dogevents/"); var dogEventSerialized = JsonConvert.SerializeObject(model); message.Content = new StringContent(dogEventSerialized, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage responseMessage = client.SendAsync(message).Result; // await client.SendAsync(message) if (responseMessage.IsSuccessStatusCode) //200 OK { //display info message.Dispose(); var responseData = responseMessage.Content.ReadAsStringAsync().Result; var definition = new { DogId = "", EventId = "" }; var ids = JsonConvert.DeserializeAnonymousType(responseData, definition); return(RedirectToAction("DogEvent", new { dogId = ids.DogId, eventId = ids.EventId })); //return View("Dog", responseMessage.Content); } else // msg why not ok { message.Dispose(); ViewBag.Message = responseMessage.StatusCode; return(View("Error")); } }