Commit 6cbf4a6a authored by Matija Obreza's avatar Matija Obreza

Merge branch '394-cms-activity-post-api-v1' into 'master'

Resolve "CMS Activity post API v1"

Closes #394

See merge request genesys-pgr/genesys-server!314
parents 458f25f4 029c3743
......@@ -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;
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