private IEnumerable <NationalPlaces.Models.Place> GetNearPlaces(double longitude, double latitude) { var scale = Math.PI / 180; var earthRadius = 6371; var allPlaces = NationalPlacesDAL.Get <NationalPlaces.Models.Place>("PlaceInformation").ToList() .Where(x => Math.Acos(Math.Sin(x.Latitude * scale) * Math.Sin(latitude * scale) + Math.Cos(x.Latitude * scale) * Math.Cos(latitude * scale) * Math.Cos(x.Longitude * scale - longitude * scale)) * earthRadius < 3); return(allPlaces); }
public IEnumerable <PlaceDto> AllPlaces() { var operationResult = this.PerformOperationAndHandleExceptions(() => { var allPlaces = NationalPlacesDAL .Get <NationalPlaces.Models.Place>("PlaceInformation") .Select(PlaceDto.FromPlace); return(allPlaces); }); return(operationResult); }
public HttpResponseMessage Logout([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var user = NationalPlacesDAL.Get <User>("UsersInformation").FirstOrDefault(x => x.SessionKey == sessionKey); if (user != null) { user.SessionKey = null; NationalPlacesDAL.SaveEntity(user, "UsersInformation"); return(Request.CreateResponse(HttpStatusCode.OK)); } else { throw new InvalidOperationException("User or password is incorrect."); } }); return(operationResult); }
public IEnumerable <CommentDto> GetComments( [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, int identifier) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var user = NationalPlacesDAL.Get <NationalPlaces.Models.User>("UsersInformation") .FirstOrDefault(x => x.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("You need to be logged in to view comments."); } var place = NationalPlacesDAL .Get <NationalPlaces.Models.Place>("PlaceInformation") .Where(x => x.PlaceIndentifierNumber == identifier) .FirstOrDefault(); if (place == null) { throw new ArgumentException("place not found"); } List <CommentDto> comments = new List <CommentDto>(); foreach (var comment in place.Comments) { if (comment.UserNickName == user.NickName) { comments.Add(new CommentDto() { Author = comment.UserNickName, Content = comment.Text }); } } comments.Reverse(); return(comments); }); return(operationResult); }
public IEnumerable <int> MyPlaces([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var user = NationalPlacesDAL.Get <NationalPlaces.Models.User>("UsersInformation") .FirstOrDefault(x => x.SessionKey == sessionKey); if (user != null) { var visitedPlaces = user.VisitedPlaces; return(visitedPlaces); } else { throw new InvalidOperationException("User or password is incorrect."); } }); return(operationResult); }
public PlaceDetailsDto Details(int identifier) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var place = NationalPlacesDAL .Get <NationalPlaces.Models.Place>("PlaceInformation") .Where(x => x.PlaceIndentifierNumber == identifier) .Select(PlaceDetailsDto.FromPlace) .FirstOrDefault(); if (place == null) { throw new ArgumentException("This place is not registered in the database"); } return(place); }); return(operationResult); }
public HttpResponseMessage CommentPlace([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, CommentPlace comment) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var user = NationalPlacesDAL.Get <NationalPlaces.Models.User>("UsersInformation") .FirstOrDefault(x => x.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("User or password is incorrect."); } double longitude = 0; double latitude = 0; DecryptCoordinateToken(comment.LocationToken, user.AuthCode, ref longitude, ref latitude); var avaiablePlaces = GetNearPlaces(longitude, latitude); if (avaiablePlaces == null || avaiablePlaces.Count() == 0) { throw new InvalidOperationException("There are no places near by."); } var placeToComment = avaiablePlaces.Where(x => x.PlaceIndentifierNumber == comment.PlaceIndentifierNumber).FirstOrDefault(); if (placeToComment == null) { throw new InvalidOperationException("You cant comment this place. It is not near you."); } var newComment = new NationalPlaces.Models.Comment() { UserNickName = user.NickName, Text = comment.Content }; placeToComment.Comments.Add(newComment); NationalPlacesDAL.SaveEntity(placeToComment, "PlaceInformation"); return(Request.CreateResponse(HttpStatusCode.OK)); }); return(operationResult); }
public HttpResponseMessage VisitPlace([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, VititPlaceDto placeToVisit) { var operationResult = this.PerformOperationAndHandleExceptions(() => { var user = NationalPlacesDAL.Get <NationalPlaces.Models.User>("UsersInformation") .FirstOrDefault(x => x.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("User or password is incorrect."); } if (placeToVisit == null) { throw new InvalidOperationException("Token Validation failed"); } double longitude = 0; double latitude = 0; DecryptCoordinateToken(placeToVisit.CoordsToken, user.AuthCode, ref longitude, ref latitude); // parse coordinates // get places by coordinates var placeToVIsit = GetNearPlaces(longitude, latitude) .Where(x => x.PlaceIndentifierNumber == placeToVisit.PlaceId) .Select(x => x.PlaceIndentifierNumber) .FirstOrDefault(); if (placeToVIsit == 0) { throw new InvalidOperationException("This place is not near you!"); } user.VisitedPlaces.Add(placeToVIsit); NationalPlacesDAL.SaveEntity(user, "UsersInformation"); return(Request.CreateResponse(HttpStatusCode.OK)); }); return(operationResult); }
public HttpResponseMessage Login(UserLogInDto loginInformation) { var operationResult = this.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid && loginInformation != null) { var existingUser = NationalPlacesDAL.Get <User>("UsersInformation") .FirstOrDefault(x => x.UserName == loginInformation.UserName.ToLower() && x.AuthCode == loginInformation.AuthCode); if (loginInformation == null || existingUser == null) { throw new InvalidOperationException("User or password is incorrent"); } if (existingUser.SessionKey == null) { existingUser.SessionKey = this.GenerateSessionKey(existingUser.Id.Value.Pid); NationalPlacesDAL.SaveEntity(existingUser, "UsersInformation"); } var loginInforrmation = UserLoggedInDto.FromUser.Compile()(existingUser); var response = Request.CreateResponse(HttpStatusCode.Created, loginInforrmation); return(response); } else { var errors = String.Join("\n ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input was not validated:\n {0}", errors); throw new ArgumentException(errorMessage); } }); return(operationResult); }
public HttpResponseMessage Register(UserRegisterDto registerDto) { var operationResult = this.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid && registerDto != null) { var existingUser = NationalPlacesDAL.Get <User>("UsersInformation") .FirstOrDefault(x => x.UserName == registerDto.UserName.ToLower() || x.NickName.ToLower() == registerDto.NickName.ToLower()); if (existingUser != null) { throw new InvalidOperationException("User name or nickname is already taken"); } var newUser = UserRegisterDto.CreateUser(registerDto); NationalPlacesDAL.Add(newUser, "UsersInformation"); newUser.SessionKey = this.GenerateSessionKey(newUser.Id.Value.Pid); NationalPlacesDAL.SaveEntity(newUser, "UsersInformation"); var loginInforrmation = UserLoggedInDto.FromUser.Compile()(newUser); var response = Request.CreateResponse(HttpStatusCode.Created, loginInforrmation); return(response); } else { var errors = String.Join("\n ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input was not validated:\n {0}", errors); throw new ArgumentException(errorMessage); } }); return(operationResult); }