Commit 029c3743 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

CMS Activity post API v1

minor fix
parent 458f25f4
......@@ -41,6 +41,7 @@ import org.genesys2.server.model.impl.Menu;
import org.genesys2.server.model.impl.MenuItem;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.filter.ActivityPostFilter;
import org.genesys2.server.service.filter.ArticleFilter;
import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.transifex.client.TransifexException;
......@@ -199,6 +200,28 @@ public class CMSController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, contentService.listArticles(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
/**
* List activity posts by filterCode or filter
*
* @param page the page
* @param filterCode short filter code
* @param filter the activity post filter
* @return the page
* @throws IOException
*/
@RequestMapping(value = "/activity-posts", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<ActivityPost> listActivityPosts(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) ActivityPostFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, ActivityPostFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<ActivityPostFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, ActivityPostFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, contentService.listActivityPosts(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
/**
* Create the activity post.
*
......@@ -207,7 +230,7 @@ public class CMSController {
*/
@RequestMapping(value = "/create-post", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public ActivityPost createActivityPost(@RequestBody final ActivityPost post) {
return contentService.createActivityPost(post.getTitle(), post.getTitle());
return contentService.createActivityPost(post.getTitle(), post.getBody());
}
/**
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.model.impl;
......@@ -27,12 +27,12 @@ import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.genesys2.server.model.AuditedModel;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.hibernate.annotations.Type;
@Entity
@Table(name = "activitypost")
public class ActivityPost extends AuditedModel {
public class ActivityPost extends AuditedVersionedModel {
private static final long serialVersionUID = 8690395020204070378L;
......
......@@ -18,7 +18,8 @@ package org.genesys2.server.persistence;
import org.genesys2.server.model.impl.ActivityPost;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
public interface ActivityPostRepository extends JpaRepository<ActivityPost, Long> {
public interface ActivityPostRepository extends JpaRepository<ActivityPost, Long>, QueryDslPredicateExecutor<ActivityPost> {
}
......@@ -28,6 +28,7 @@ import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Menu;
import org.genesys2.server.model.impl.MenuItem;
import org.genesys2.server.service.filter.ActivityPostFilter;
import org.genesys2.server.service.filter.ArticleFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -148,6 +149,8 @@ public interface ContentService {
ActivityPost updateActivityPost(long id, String title, String body) throws NotFoundElement;
Page<ActivityPost> listActivityPosts(ActivityPostFilter filter, Pageable pageable);
void deleteActivityPost(long id);
/**
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QActivityPost.activityPost;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.catalog.model.filters.FilterHelpers;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.QActivityPost;
/**
* Filters for {@link ActivityPost}.
*
* @author Maxym Borodenko
*/
public class ActivityPostFilter extends AuditedVersionedModelFilter<ActivityPostFilter, ActivityPost> {
/** Any text. */
public String _text;
@Override
public Predicate buildQuery() {
return buildQuery(activityPost);
}
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery(QActivityPost activityPost) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(activityPost, activityPost._super._super, and);
if (StringUtils.isNotBlank(_text)) {
/*@formatter:off*/
and.andAnyOf(
ArrayUtils.addAll(
FilterHelpers.equalsAny(_text, activityPost.title, activityPost.body),
FilterHelpers.containsAll(_text, activityPost.title, activityPost.body)
)
);
/*@formatter:on*/
}
return and;
}
}
......@@ -41,6 +41,7 @@ import org.genesys2.server.persistence.MenuRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.filter.ActivityPostFilter;
import org.genesys2.server.service.filter.ArticleFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -67,6 +68,9 @@ public class ContentServiceImpl implements ContentService {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ActivityPostRepository activityPostRepository;
@Autowired
private ClassPKService classPkService;
......@@ -297,6 +301,11 @@ public class ContentServiceImpl implements ContentService {
return updatePostData(post, title, body);
}
@Override
public Page<ActivityPost> listActivityPosts(ActivityPostFilter filter, Pageable pageable) {
return activityPostRepository.findAll(filter.buildQuery(), pageable);
}
private ActivityPost updatePostData(ActivityPost post, String title, String body) {
post.setTitle(htmlSanitizer.sanitize(title));
post.setBody(htmlSanitizer.sanitize(body));
......
......@@ -4615,3 +4615,32 @@ databaseChangeLog:
- column:
name: link
type: varchar(100)
- changeSet:
id: 1545647651507-1
author: mborodenko
comment: Add `active` and `version` to CMS ActivityPost
changes:
- addColumn:
tableName: activitypost
columns:
- column:
constraints:
nullable: false
defaultValue: 0
name: version
type: INT
- column:
constraints:
nullable: false
defaultValue: true
name: active
type: BIT(1)
- changeSet:
id: 1545647651507-2
author: mborodenko
changes:
- sql:
comment: Assign activitypost.version
sql: update activitypost set version = 1 where version = 0;
......@@ -17,7 +17,6 @@
package org.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
......@@ -32,18 +31,14 @@ import org.genesys2.server.persistence.ArticleRepository;
import org.genesys2.server.persistence.MenuItemRepository;
import org.genesys2.server.persistence.MenuRepository;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.filter.ActivityPostFilter;
import org.genesys2.server.service.filter.ArticleFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
......@@ -54,14 +49,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
public class CMSControllerTest extends AbstractApiTest {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");
private static final String ACTIVITY_POST_TITLE = "title of activity post";
@Autowired
private ContentService contentService;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private ActivityPostRepository postRepository;
@Autowired
private ArticleRepository articleRepository;
......@@ -70,7 +62,6 @@ public class CMSControllerTest extends AbstractApiTest {
@Autowired
private MenuItemRepository repoMenuItem;
private MockMvc mockMvc;
private Article testArticle;
protected static final ObjectMapper objectMapper;
......@@ -98,11 +89,8 @@ public class CMSControllerTest extends AbstractApiTest {
@Transactional
public void beforeTest() throws Exception {
super.beforeTest();
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(documentationConfiguration(this.restDocumentation).uris().withScheme("https").withHost(
"sandbox.genesys-pgr.org").withPort(443)).build();
// create activity post for test
contentService.createActivityPost("title of activity post", "body of activity post");
contentService.createActivityPost(ACTIVITY_POST_TITLE, "body of activity post");
// create article for test
testArticle = contentService.createGlobalArticle(ContentService.SMTP_EMAIL_VERIFICATION, contentService.getDefaultLocale(), "title of article", "<h3>body of article</h3>", null, false);
......@@ -112,12 +100,13 @@ public class CMSControllerTest extends AbstractApiTest {
@Test
public void getLastNewsTest() throws Exception {
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/last-news"))
mockMvc
.perform(get(CMSController.API_BASE.concat("/last-news"))
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.*", hasSize(1)));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.*", hasSize(1)));
/*@formatter:on*/
}
......@@ -126,12 +115,13 @@ public class CMSControllerTest extends AbstractApiTest {
contentService.createActivityPost("title of activity post 2", "body of activity post 2");
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/all-news"))
mockMvc
.perform(get(CMSController.API_BASE.concat("/all-news"))
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.totalElements", is(2)));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.totalElements", is(2)));
/*@formatter:on*/
}
......@@ -142,53 +132,56 @@ public class CMSControllerTest extends AbstractApiTest {
testArticle = contentService.updateArticle(Article.class, 1L, testArticle.getSlug(), testArticle.getTitle(), testArticle.getBody(), "", new Locale(testArticle.getLang()));
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/{slug}/{clazz}/{id}/{lang}"), testArticle.getSlug(), clazz, testArticle.getTargetId().toString(), testArticle.getLang())
mockMvc
.perform(get(CMSController.API_BASE.concat("/{slug}/{clazz}/{id}/{lang}"), testArticle.getSlug(), clazz, testArticle.getTargetId().toString(), testArticle.getLang())
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.targetId", is(testArticle.getTargetId().intValue())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.targetId", is(testArticle.getTargetId().intValue())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
/*@formatter:on*/
}
@Test
public void getArticleBySlugAndLangTest() throws Exception {
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/article/{slug}/{lang}"), testArticle.getSlug(), testArticle.getLang())
mockMvc
.perform(get(CMSController.API_BASE.concat("/article/{slug}/{lang}"), testArticle.getSlug(), testArticle.getLang())
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
/*@formatter:on*/
}
@Test
public void getGlobalArticleTest() throws Exception {
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/global-article/{slug}/{lang}"), testArticle.getSlug(), testArticle.getLang())
mockMvc
.perform(get(CMSController.API_BASE.concat("/global-article/{slug}/{lang}"), testArticle.getSlug(), testArticle.getLang())
.param("useDefault", "true")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(testArticle.getSlug())))
.andExpect(jsonPath("$.title", is(testArticle.getTitle())))
.andExpect(jsonPath("$.body", is(testArticle.getBody())))
.andExpect(jsonPath("$.template", is(testArticle.isTemplate())))
.andExpect(jsonPath("$.lang", is(testArticle.getLang())));
/*@formatter:on*/
}
......@@ -199,14 +192,34 @@ public class CMSControllerTest extends AbstractApiTest {
filter.slug.add(testArticle.getSlug());
/*@formatter:off*/
mockMvc.perform(post(CMSController.API_BASE.concat("/list"))
mockMvc
.perform(post(CMSController.API_BASE.concat("/list"))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].id", is(testArticle.getId().intValue())));
/*@formatter:on*/
}
@Test
public void listActivityPostsTest() throws Exception {
final ActivityPostFilter filter = new ActivityPostFilter();
filter._text = ACTIVITY_POST_TITLE.substring(0, 10);
/*@formatter:off*/
mockMvc
.perform(post(CMSController.API_BASE.concat("/activity-posts"))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].id", is(testArticle.getId().intValue())));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content[0].title", is(ACTIVITY_POST_TITLE)));
/*@formatter:on*/
}
......@@ -216,13 +229,14 @@ public class CMSControllerTest extends AbstractApiTest {
contentService.ensureMenuItem(menuKey, "testURL", "testText");
/*@formatter:off*/
mockMvc.perform(get(CMSController.API_BASE.concat("/menu/{menuKey}"), "testMenuKey")
mockMvc
.perform(get(CMSController.API_BASE.concat("/menu/{menuKey}"), "testMenuKey")
.contentType(MediaType.APPLICATION_JSON_UTF8))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.key", is(menuKey)));
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.key", is(menuKey)));
/*@formatter:on*/
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment