/// <summary>
        ///     This method updates the specified entity excluding the user object.
        /// </summary>
        /// <param name="entity">The entity parameter represents the updated project object.</param>
        public override void Update(Project entity)
        {
            entity = UpdateUpdatedField(entity);

            DbSet.Attach(entity);
            if (entity.User != null)
            {
                DbContext.Entry(entity.User)
                .Property(x => x.Email)
                .IsModified = false;

                DbContext.Entry(entity.User)
                .State = EntityState.Unchanged;
            }

            if (entity.ProjectIcon == null)
            {
                DbContext.Entry(entity)
                .Entity.ProjectIconId = null;
            }

            DbSet.Update(entity);
            SetLikes(entity);
            ESProjectDTO projectToSync = ProjectConverter.ProjectToESProjectDTO(entity);

            taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(projectToSync), Subject.ELASTIC_CREATE_OR_UPDATE);
        }
        /// <summary>
        /// This method removes the project from the database and ES Index.
        /// </summary>
        /// <param name="entity">The entity parameter represents a project to be removed.</param>
        public override void Remove(Project entity)
        {
            base.Remove(entity);
            SetLikes(entity);
            ESProjectDTO projectToSync = ProjectConverter.ProjectToESProjectDTO(entity);

            taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(projectToSync), Subject.ELASTIC_DELETE);
        }
        /// <summary>
        ///     This method redacts user email from the Project if isPublic setting is set to false.
        /// This method asynchronously removes the project from the database and ES Index coupled to the given id.
        /// </summary>
        /// <param name="id">The project parameter represents unique identifier.</param>
        /// <returns>
        /// The task wherein the removal of the project from the database is executed.
        /// </returns>
        public override Task RemoveAsync(int id)
        {
            Project entity = DbSet.Find(id);

            SetLikes(entity);
            ESProjectDTO projectToSync = ProjectConverter.ProjectToESProjectDTO(entity);

            taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(projectToSync), Subject.ELASTIC_DELETE);
            return(base.RemoveAsync(id));
        }
        /// <summary>
        /// This method adds the given project to the database and ES Index.
        /// </summary>
        /// <param name="entity">The entity parameter represents a project to be added.</param>
        public override void Add(Project entity)
        {
            base.Add(entity);
            base.Save();
            Console.WriteLine("Add method id: " + entity.Id);
            SetLikes(entity);
            ESProjectDTO projectToSync = ProjectConverter.ProjectToESProjectDTO(entity);

            taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(projectToSync), Subject.ELASTIC_CREATE_OR_UPDATE);
        }
        public static List <ESProjectDTO> ProjectsToProjectESDTOs(List <Project> projectsToConvert)
        {
            List <ESProjectDTO> convertedProjects = new List <ESProjectDTO>();

            foreach (Project project in projectsToConvert)
            {
                ESProjectDTO convertedProject = ProjectToESProjectDTO(project);
                convertedProjects.Add(convertedProject);
            }
            return(convertedProjects);
        }
        public async Task SyncProjectToES(Project project)
        {
            Project projectToSync = await FindAsync(project.Id);

            if (projectToSync == null)
            {
                throw new NotFoundException("Project to sync was not found");
            }
            ESProjectDTO eSProjectDTO = ProjectConverter.ProjectToESProjectDTO(projectToSync);

            taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(eSProjectDTO), Subject.ELASTIC_CREATE_OR_UPDATE);
        }
        private List <ESProjectDTO> ProjectsToProjectESDTO(List <Project> projects)
        {
            List <ESProjectDTO> convertedProjects = new List <ESProjectDTO>();

            foreach (Project project in projects)
            {
                ESProjectDTO convertedProject = new ESProjectDTO();
                ProjectToEsProjectESDTO(project, convertedProject);
                convertedProjects.Add(convertedProject);
            }
            return(convertedProjects);
        }
 /// <summary>
 ///     Parses the payload.
 /// </summary>
 public void ParsePayload(string jsonBody)
 {
     try
     {
         Log.Logger.Information("Document updater");
         Log.Logger.Information("Json payload: " + jsonBody);
         eSProject = JsonConvert.DeserializeObject <ESProjectDTO>(jsonBody);
     } catch (Exception e)
     {
         Log.Logger.Error("Failed: " + e.Message);
         Log.Logger.Information(jsonBody);
     }
 }
Exemplo n.º 9
0
        public ESProjectDTO JsonStringToProjectES(string body)
        {
            JToken       token   = JToken.Parse(body);
            ESProjectDTO project = new ESProjectDTO();

            project.Description = token.Value <string>("Description");
            project.ProjectName = token.Value <string>("Name");
            project.Id          = token.Value <int>("Id");
            project.Created     = token.Value <DateTime>("Created");
            project.Likes       = token.Value <List <int> >("DescLikesription");

            return(project);
        }
        public void SyncProjectToESTest_Badflow([ProjectDataSource(1)] Project project)
        {
            ESProjectDTO dto = new ESProjectDTO();

            ProjectToEsProjectESDTO(project, dto);
            string  payload = Newtonsoft.Json.JsonConvert.SerializeObject(dto);
            Subject subject = Subject.ELASTIC_CREATE_OR_UPDATE;


            TaskPublisher.Setup(x => x.RegisterTask(It.Is <string>(x => x == payload), It.Is <Subject>(x => x == subject))).Verifiable();


            Assert.ThrowsAsync <NotFoundException>(async() => await Repository.SyncProjectToES(project));
        }
        private void ProjectToEsProjectESDTO(Project project, ESProjectDTO dto)
        {
            List <int> likes = new List <int>();

            if (project.Likes != null)
            {
                foreach (ProjectLike projectLike in project.Likes)
                {
                    likes.Add(projectLike.UserId);
                }
            }
            dto.Description = project.Description;
            dto.ProjectName = project.Name;
            dto.Id          = project.Id;
            dto.Created     = project.Created;
            dto.Likes       = likes;
        }
        public override void AddRange(IEnumerable <Project> entities)
        {
            List <Project> entityList = entities.ToList();

            for (int i = 0; i < entityList.Count; i++)
            {
                entityList[i] = UpdateCreatedField(entityList[i]);
                entityList[i] = UpdateUpdatedField(entityList[i]);
            }
            DbSet.AddRange(entityList);
            entityList.ForEach(x =>
            {
                SetLikes(x);
                ESProjectDTO projectToSync = ProjectConverter.ProjectToESProjectDTO(x);
                taskPublisher.RegisterTask(Newtonsoft.Json.JsonConvert.SerializeObject(projectToSync),
                                           Subject.ELASTIC_CREATE_OR_UPDATE);
            });
        }
        public async Task SyncProjectToESTest_Goodflow([ProjectDataSource(1)] Project project)
        {
            DbContext.Add(project);
            await DbContext.SaveChangesAsync();

            ESProjectDTO dto = new ESProjectDTO();

            ProjectToEsProjectESDTO(project, dto);
            string  payload = Newtonsoft.Json.JsonConvert.SerializeObject(dto);
            Subject subject = Subject.ELASTIC_CREATE_OR_UPDATE;

            TaskPublisher.Setup(x => x.RegisterTask(It.Is <string>(x => x == payload), It.Is <Subject>(x => x == subject))).Verifiable();


            await Repository.SyncProjectToES(project);

            TaskPublisher.VerifyAll();
        }
        public static ESProjectDTO ProjectToESProjectDTO(Project project)
        {
            ESProjectDTO convertedProject = new ESProjectDTO();
            List <int>   likes            = new List <int>();

            if (project.Likes != null)
            {
                foreach (ProjectLike projectLike in project.Likes)
                {
                    likes.Add(projectLike.UserId);
                }
            }
            convertedProject.Description = project.Description;
            convertedProject.ProjectName = project.Name;
            convertedProject.Id          = project.Id;
            convertedProject.Created     = project.Created;
            convertedProject.Likes       = likes;
            return(convertedProject);
        }