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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }