Ejemplo n.º 1
0
 private dynamic PostPhoto(dynamic parameters)
 {
     try
     {
         var userName = (string)Request.Session[ControllerUser.SessionUserNameKey];
         if (string.IsNullOrEmpty(userName))
         {
             return(Response.AsJson("Noone logged in.", HttpStatusCode.Unauthorized));
         }
         var db   = new MainContext();
         var user = ServiceUser.GetLoggedUser(userName, db);
         if (user == null)
         {
             return(Response.AsJson("Logged user with bad userName", HttpStatusCode.InternalServerError));
         }
         var body = this.Bind <PostPhotoBody>();
         if (string.IsNullOrEmpty(body.Url))
         {
             return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest));
         }
         var placeId = parameters.id;
         var place   = ServicePlace.GetPlaceById(placeId, db);
         if (place == null)
         {
             return(Response.AsJson("Place with this id not found.", HttpStatusCode.NotFound));
         }
         ModelPhoto photo = ServicePlace.AddPhoto(place, user, body.Url, db);
         return(Response.AsJson(photo.GetView()));
     }
     catch (InDataError)
     {
         return(HttpStatusCode.InternalServerError);
     }
     catch (ModelBindingException)
     {
         return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest));
     }
     catch (UnauthorizedAccessException)
     {
         return(Response.AsJson("You are not author of this place.", HttpStatusCode.Unauthorized));
     }
 }
Ejemplo n.º 2
0
 private dynamic PostPlace(dynamic parameters)
 {
     try
     {
         var userName = (string)this.Request.Session[ControllerUser.SessionUserNameKey];
         if (string.IsNullOrEmpty(userName))
         {
             Console.WriteLine("No i dupa");
             return(Response.AsJson("Noone logged in.", HttpStatusCode.NotFound));
         }
         var db   = new MainContext();
         var user = ServiceUser.GetLoggedUser(userName, db);
         if (user == null)
         {
             return(Response.AsJson("Logged user with bad userName", HttpStatusCode.InternalServerError));
         }
         var body = this.Bind <PostPlaceBody>();
         if (string.IsNullOrEmpty(body.Name) ||
             string.IsNullOrEmpty(body.Description) ||
             string.IsNullOrEmpty(body.Address) ||
             body.Latitude == null ||
             body.Longitude == null ||
             body.Photos == null)
         {
             return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest));
         }
         var place = ServicePlace.CreatePlace(body.Name, body.Description, body.Address, (double)body.Latitude, (double)body.Longitude, user, db);
         foreach (var link in body.Photos)
         {
             ServicePlace.AddPhoto(place, user, link, db);
         }
         return(Response.AsJson(place.GetView()));
     }
     catch (InDataError)
     {
         return(HttpStatusCode.InternalServerError);
     }
     catch (ModelBindingException)
     {
         return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest));
     }
 }