api for adding a new community

This commit is contained in:
Michele Artini 2023-07-10 10:42:11 +02:00
parent 043cb4497a
commit a8f3bb6961
2 changed files with 39 additions and 1 deletions

View File

@ -62,6 +62,21 @@ public class CommunityApiController {
return communityService.listCommunities();
}
@RequestMapping(value = "/community", produces = {
"application/json"
}, method = RequestMethod.POST)
@Operation(summary = "add a new community profile", description = "add a new community profile", tags = {
C, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public CommunityDetails getCommunity(@RequestBody final CommunityDetails details) throws CommunityException {
return communityService.newCommunity(details);
}
@RequestMapping(value = "/community/{id}", produces = {
"application/json"
}, method = RequestMethod.GET)

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.community;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
@ -11,6 +12,7 @@ import java.util.stream.Collectors;
import javax.transaction.Transactional;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -77,9 +79,23 @@ public class CommunityService {
}
}
@Transactional
public CommunityDetails newCommunity(final CommunityDetails details) throws CommunityException {
if (StringUtils.isBlank(details.getId())) {
throw new CommunityException("Empty Id");
} else if (dbCommunityRepository.existsById(details.getId())) {
throw new CommunityException("Community already exists: " + details.getId());
} else {
details.setCreationDate(LocalDateTime.now());
return saveCommunity(details);
}
}
@Transactional
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
try {
details.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details));
return getCommunity(details.getId());
} catch (final Throwable e) {
@ -104,6 +120,7 @@ public class CommunityService {
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
c.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(c);
} catch (final Throwable e) {
log.error(e);
@ -338,9 +355,9 @@ public class CommunityService {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(advancedCosntraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
@ -352,6 +369,7 @@ public class CommunityService {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
@ -365,6 +383,7 @@ public class CommunityService {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(removeConstraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
@ -378,6 +397,7 @@ public class CommunityService {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
@ -416,6 +436,7 @@ public class CommunityService {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
setter.accept(dbEntry, value);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
@ -448,6 +469,8 @@ public class CommunityService {
setter.accept(dbEntry, tmpList.toArray(new String[tmpList.size()]));
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);