새 컬렉션 추가, 로딩, 새 요청 추가
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.eactive.httpmockserver.client.service;
|
||||
|
||||
import com.eactive.httpmockserver.client.entity.ApiCollection;
|
||||
import com.eactive.httpmockserver.client.entity.ApiRequestInfo;
|
||||
import com.eactive.httpmockserver.client.repository.ApiCollectionRepository;
|
||||
import com.eactive.httpmockserver.client.repository.ApiRequestRepository;
|
||||
import com.eactive.httpmockserver.common.exception.NotFoundException;
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ApiRequestMgmtService {
|
||||
|
||||
@Autowired
|
||||
ApiCollectionRepository apiCollectionRepository;
|
||||
|
||||
@Autowired
|
||||
ApiRequestRepository apiRequestRepository;
|
||||
|
||||
|
||||
public List<ApiCollection> getMyApis(StaffUser user) {
|
||||
return apiCollectionRepository.findAllByOwner(user);
|
||||
}
|
||||
|
||||
public void deleteApiCollection(String id) {
|
||||
apiCollectionRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public void createNewApiCollection(ApiCollection collection) {
|
||||
collection.setId(UUID.randomUUID().toString());
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
public void updateApiCollection(String id, ApiCollection updated) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
collection.setName(updated.getName());
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
public String saveApiToCollection(String id, ApiRequestInfo api) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
|
||||
if (StringUtils.isEmpty(api.getId())) {
|
||||
api.setId(UUID.randomUUID().toString());
|
||||
apiRequestRepository.save(api);
|
||||
} else {
|
||||
String apiId = api.getId();
|
||||
ApiRequestInfo existing = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException("api[" + apiId + "] not found"));
|
||||
existing.setName(api.getName());
|
||||
existing.setMethod(api.getMethod());
|
||||
existing.setPath(api.getPath());
|
||||
existing.setHeaders(api.getHeaders());
|
||||
existing.setQueryParams(api.getQueryParams());
|
||||
existing.setServer(api.getServer());
|
||||
existing.setRequestBody(api.getRequestBody());
|
||||
apiRequestRepository.save(existing);
|
||||
api = existing;
|
||||
}
|
||||
|
||||
|
||||
if (!collection.getApis().contains(api)) {
|
||||
collection.getApis().add(api);
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
return api.getId();
|
||||
}
|
||||
|
||||
public void removeApiFromCollection(String id, String apiId) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
ApiRequestInfo api = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException("api[" + apiId + "] not found"));
|
||||
|
||||
collection.getApis().remove(api);
|
||||
apiCollectionRepository.save(collection);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user