public void AddItem(int Id)
 {
     using (var context = new ScrapeCityDbContext())
     {
         _index.AddObject(JObject.Parse(JsonConvert.SerializeObject(context.Monitors.Find(Id), Formatting.Indented, converters: new Newtonsoft.Json.Converters.StringEnumConverter())));
     }
 }
Ejemplo n.º 2
0
 public IActionResult Register(RegisterViewModel model)
 {
     if (ModelState.IsValid && model != null)
     {
         if (model.Email == null || model.Email == "" || model.Firstname == null ||
             model.Firstname == "" || model.Lastname == null || model.Lastname == "" || model.Password == null || model.Password == "")
         {
             ViewData["Error"] = "Field missing";
             return(RedirectToAction("Index"));
         }
         try
         {
             Core.Models.User user = new Core.Models.User
             {
                 Email       = model.Email,
                 Password    = Users.HashPassword(model.Password),
                 Firstname   = model.Firstname,
                 Lastname    = model.Lastname,
                 Nickname    = $"{model.Firstname.ToLower()}.{model.Lastname.ToLower()}",
                 Description = "",
                 Gender      = "",
                 Phone       = "",
                 Picture     = "",
                 AccessToken = "",
                 Private     = false
             };
             var userInDb = DataAccess.User.Add(user);
             if (userInDb == null)
             {
                 ViewData["Error"] = "Impossible to save the current user";
                 return(RedirectToAction("Index"));
             }
             userInDb.objectID = user.Id.ToString();
             AlgoliaClient client = new AlgoliaClient(_configuration.GetValue <string>("Algolia:AppId"),
                                                      _configuration.GetValue <string>("Algolia:AppSecret"));
             Index usersIndex = client.InitIndex(_configuration.GetValue <string>("Algolia:IndexUser"));
             usersIndex.AddObject(userInDb);
             DataAccess.User.Update(userInDb);
             Helper.AppHttpContext.HttpContext.Session.SetObject <long>("currentUserId", userInDb.Id);
             return(Redirect(_configuration.GetValue <string>("RedirectFront")));
         }
         catch (Exception e)
         {
             ViewData["Error"] = "Impossible to save the current user\n" + e.Message;
             return(RedirectToAction("Index"));
         }
     }
     else
     {
         return(BadRequest());
     }
 }
Ejemplo n.º 3
0
        public void IndexPageItem(IndexingQueueItem queueItem)
        {
            Index index = this.Client.InitIndex(this.GetPagesIndexName(queueItem.Language));

            if (!queueItem.Deleted)
            {
                Item item = this.database.GetItem(ID.Parse(queueItem.Id), Language.Parse(queueItem.Language));

                index.AddObject(this.crawler.GetJsonForItem(item));
            }
            else
            {
                index.DeleteObject(this.crawler.GetObjectId(queueItem.Id));
            }
        }
Ejemplo n.º 4
0
        public async Task TestAddObject()
        {
            await clearTest();

            var task = await _index.AddObject(JObject.Parse(@"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"));

            await _index.WaitTask(task["taskID"].ToString());

            var res = await _index.Search(new Query(""));

            Assert.AreEqual(1, res["nbHits"].ToObject <int>());
            Assert.AreEqual("Jimmie", res["hits"][0]["firstname"].ToString());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> FacebookLoginCallback(string returnUrl = null, string remoteError = null)
        {
            string codeString  = HttpContext.Request.Query["code"].ToString();
            string stateString = HttpContext.Request.Query["state"].ToString();

            if (codeString == null || stateString == null || codeString == "" || stateString == "")
            {
                _logger?.LogInformation($"Impossible to connect. Code: { codeString } and state { stateString } are invalid");
                return(RedirectToAction("Index"));
            }
            string url = "https://graph.facebook.com/v3.0/oauth/access_token?client_id=";

            url += _configuration.GetValue <string>("Facebook:AppId");
            url += "&redirect_uri=";
            url += _configuration.GetValue <string>("Facebook:redirectUri") + "&client_secret=";
            url += _configuration.GetValue <string>("Facebook:AppSecret");
            url += "&code=" + codeString;

            string accessToken = await GetAccessToken(url);

            if (accessToken == null || accessToken == "")
            {
                _logger?.LogInformation($"Impossible to connect. Access Token: { accessToken } is invalid");
                return(RedirectToAction("Index"));
            }

            var client  = new Facebook.Client.FacebookClient();
            var service = new Facebook.Service.FacebookService(client);
            var account = await service.GetAccountAsync(accessToken);

            if (account == null || account.Email == null || account.Email == "")
            {
                _logger?.LogInformation($"Impossible to connect. Impossible to get account information");
                return(RedirectToAction("Index"));
            }

            var currentUser = GetApplicationUser(account, accessToken);
            var userDB      = GetTypeUser(currentUser);

            userDB.Password    = Users.HashPassword(accessToken);
            userDB.AccessToken = accessToken;
            try
            {
                var userInDb = await DataAccess.User.GetFromEmail(userDB.Email);

                if (userInDb == null)
                {
                    //First connection
                    userDB = DataAccess.User.Add(userDB);
                    AlgoliaClient algolia = new AlgoliaClient(_configuration.GetValue <string>("Algolia:AppId"),
                                                              _configuration.GetValue <string>("Algolia:AppSecret"));
                    Index usersIndex = algolia.InitIndex(_configuration.GetValue <string>("Algolia:IndexUser"));
                    userDB.objectID = userDB.Id.ToString();
                    usersIndex.AddObject(userDB);
                    DataAccess.User.Update(userDB);
                }
                else
                {
                    userDB             = userInDb;
                    userDB.Password    = Users.HashPassword(accessToken);
                    userDB.AccessToken = accessToken;
                    DataAccess.User.Update(userDB);
                }
            }
            catch (Exception e)
            {
                _logger?.LogInformation($"Impossible to connect. Impossible to save account information\n" + e.Message);
                return(RedirectToAction("Index"));
            }

            _logger?.LogInformation("User connected");

            _signInManager.Context.Session.SetObject("currentToken", accessToken);
            await _signInManager.Context.Session.CommitAsync();

            Response.Cookies.Append(".Amstramgram.Cookie", userDB.AccessToken);

            return(Redirect(_configuration.GetValue <string>("RedirectFront")));
        }
Ejemplo n.º 6
0
        public AmstramgramMutation(IMapper mapper, IConfiguration configuration)
        {
            Name = "Mutation";

            AlgoliaClient client = new AlgoliaClient(configuration.GetValue <string>("Algolia:AppId"),
                                                     configuration.GetValue <string>("Algolia:AppSecret"));
            Index usersIndex    = client.InitIndex(configuration.GetValue <string>("Algolia:IndexUser"));
            Index picturesIndex = client.InitIndex(configuration.GetValue <string>("Algolia:IndexPictures"));
            Index tagsIndex     = client.InitIndex(configuration.GetValue <string>("Algolia:IndexTags"));

            Field <UserType>(
                "createUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.User>("user");
                var user = DataAccess.User.Add(data);
                if (user != null)
                {
                    data.objectID = data.Id.ToString();
                    usersIndex.AddObject(data);
                    //Save object id
                    DataAccess.User.Update(data);
                }
                return(mapper.Map <User>(user));
            }
                );

            Field <UserType>(
                "updateUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            }
                    ),
                resolve: context =>
            {
                var data     = context.GetArgument <Core.Models.User>("user");
                var userInDb = DataAccess.User.Get(data.Id).Result;
                if (userInDb == null || userInDb.Id == 0)
                {
                    //User doesn't exist
                    return(null);
                }
                data.objectID    = userInDb.objectID;
                data.AccessToken = userInDb.AccessToken;
                bool result      = DataAccess.User.Update(data);
                if (result)
                {
                    usersIndex.PartialUpdateObject(JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(data)));
                }
                return(mapper.Map <User>(result ? data : null));
            }
                );

            Field <PictureType>(
                "createPicture",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <PictureInputType> > {
                Name = "picture"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.Picture>("picture");
                if (DataAccess.User.Get(data.UserId).Result == null)
                {
                    return(null);
                }
                var date       = ((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString();
                data.CreatedAt = date;
                data.UpdatedAt = date;
                var picture    = DataAccess.Picture.Add(data);
                foreach (var tag in data.Tags)
                {
                    tagsIndex.AddObject(tag);
                }
                data.objectID = data.Id.ToString();
                picturesIndex.AddObject(data);
                DataAccess.Picture.Update(data);
                return(mapper.Map <Picture>(picture));
            }
                );

            Field <CommentType>(
                "createComment",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <CommentInputType> > {
                Name = "comment"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.Comment>("comment");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.Picture.Get(data.PictureId).Result == null)
                {
                    return(null);
                }
                if (data.Text == "")
                {
                    return(null);
                }
                var date       = ((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString();
                data.CreatedAt = date;
                var comment    = DataAccess.Comment.Add(data);
                return(mapper.Map <Comment>(comment));
            }
                );

            Field <CommentType>(
                "deleteComment",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <CommentInputType> > {
                Name = "comment"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.Comment>("comment");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.Picture.Get(data.PictureId).Result == null)
                {
                    return(null);
                }
                DataAccess.Comment.Delete(data);
                return(mapper.Map <Comment>(data));
            }
                );

            Field <LikeType>(
                "createLike",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <LikeInputType> > {
                Name = "like"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.Like>("like");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.Picture.Get(data.PictureId).Result == null)
                {
                    return(null);
                }
                if (DataAccess.Like.Find(data.UserId, data.PictureId) != null)
                {
                    return(null);
                }
                var date       = ((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString();
                data.CreatedAt = date;
                var like       = DataAccess.Like.Add(data);
                return(mapper.Map <Like>(like));
            }
                );

            Field <LikeType>(
                "deleteLike",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <LikeInputType> > {
                Name = "like"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.Like>("like");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.Picture.Get(data.PictureId).Result == null)
                {
                    return(null);
                }
                var like = DataAccess.Like.Find(data.UserId, data.PictureId);
                if (like == null)
                {
                    return(null);
                }
                DataAccess.Like.Delete(like);
                return(mapper.Map <Like>(like));
            }
                );

            Field <UserFollowerType>(
                "createFollower",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserFollowerInputType> > {
                Name = "follower"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.UserFollower>("follower");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.User.Get(data.FollowerId).Result == null)
                {
                    return(null);
                }
                if (data.UserId == data.FollowerId)
                {
                    return(null);
                }
                if (DataAccess.UserFollower.Find(data.UserId, data.FollowerId) != null)
                {
                    return(null);
                }
                var follower = DataAccess.UserFollower.Add(data);
                return(mapper.Map <UserFollower>(follower));
            }
                );

            Field <UserFollowerType>(
                "deleteFollower",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserFollowerInputType> > {
                Name = "follower"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.UserFollower>("follower");
                if (DataAccess.User.Get(data.UserId).Result == null || DataAccess.User.Get(data.FollowerId).Result == null)
                {
                    return(null);
                }
                var follower = DataAccess.UserFollower.Find(data.UserId, data.FollowerId);
                if (follower == null)
                {
                    return(null);
                }
                DataAccess.UserFollower.Delete(follower);
                return(mapper.Map <UserFollower>(follower));
            }
                );

            Field <UserType>(
                "registerUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.User>("user");
                if (data.Email == null || data.Email == "" || data.Password == null || data.Password == "")
                {
                    return(null);
                }
                data.Password = Helper.Users.HashPassword(data.Password);
                var user      = DataAccess.User.Add(data);
                user.objectID = data.Id.ToString();
                usersIndex.AddObject(data);
                DataAccess.User.Update(data);
                Helper.AppHttpContext.HttpContext.Response.Cookies.Append(".Amstramgram.Cookie", user.Password);
                return(mapper.Map <User>(user));
            }
                );

            Field <UserType>(
                "connectUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            }
                    ),
                resolve: context =>
            {
                var data = context.GetArgument <Core.Models.User>("user");
                if (data.Email == null || data.Email == "" || data.Password == null || data.Password == "")
                {
                    return(null);
                }
                data.Password = Helper.Users.HashPassword(data.Password);
                var user      = DataAccess.User.SignInUser(data).Result;
                Helper.AppHttpContext.HttpContext.Response.Cookies.Append(".Amstramgram.Cookie", user.AccessToken);
                return(mapper.Map <User>(user));
            }
                );
        }