complete controllers
parent
8e89ec08a7
commit
938deda29b
@ -1,36 +0,0 @@
|
||||
package ru.ulstu.activity.common.service;
|
||||
|
||||
import ru.ulstu.activity.api.ActivityRepository;
|
||||
import ru.ulstu.activity.api.model.ActivityDto;
|
||||
import ru.ulstu.activity.api.model.ActivityListDto;
|
||||
import ru.ulstu.activity.common.model.AbstractActivity;
|
||||
import ru.ulstu.core.model.response.PageableItems;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
||||
|
||||
public abstract class AbstractActivityService<L extends ActivityListDto, T extends AbstractActivity, D extends ActivityDto> {
|
||||
|
||||
protected ActivityRepository activityRepository;
|
||||
|
||||
public abstract T create(T entity);
|
||||
|
||||
public abstract D create(D entity);
|
||||
|
||||
public abstract T update(T entity);
|
||||
|
||||
public abstract D update(D entity);
|
||||
|
||||
public abstract boolean delete(Integer id);
|
||||
|
||||
public abstract PageableItems<T> findAll(int offset, int count);
|
||||
|
||||
public PageableItems<L> findAllDto(int offset, int count) {
|
||||
return convertPageable(findAll(offset, count), entity -> getActivityListDto(entity));
|
||||
}
|
||||
|
||||
protected abstract L getActivityListDto(T entity);
|
||||
|
||||
protected boolean checkUniqueName(String title, Integer id) {
|
||||
return title.equals(activityRepository.findByNameAndNotId(title, id));
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ru.ulstu.activity.common.service;
|
||||
|
||||
import ru.ulstu.activity.common.model.AbstractActivity;
|
||||
|
||||
public abstract class ActivityNotificationService<T extends AbstractActivity> {
|
||||
|
||||
public abstract void sendCreateNotification(T entity);
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package ru.ulstu.activity.common.service;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.activity.api.ActivityRepository;
|
||||
import ru.ulstu.activity.api.model.ActivityDto;
|
||||
import ru.ulstu.activity.api.model.ActivityListDto;
|
||||
import ru.ulstu.activity.common.model.AbstractActivity;
|
||||
import ru.ulstu.activity.ping.service.PingService;
|
||||
import ru.ulstu.core.model.response.PageableItems;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
||||
|
||||
public abstract class ActivityService<L extends ActivityListDto, T extends AbstractActivity, D extends ActivityDto> {
|
||||
|
||||
protected final ActivityRepository<T> activityRepository;
|
||||
protected final ActivityNotificationService<T> activityNotificationService;
|
||||
protected final PingService pingService;
|
||||
|
||||
protected ActivityService(ActivityRepository activityRepository,
|
||||
ActivityNotificationService<T> activityNotificationService,
|
||||
PingService pingService) {
|
||||
this.activityRepository = activityRepository;
|
||||
this.activityNotificationService = activityNotificationService;
|
||||
this.pingService = pingService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public D create(D entityDto) {
|
||||
T newEntity;
|
||||
try {
|
||||
newEntity = copyFromDto(getNewActivity(), entityDto);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return getNewActivityDto(create(newEntity));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public T create(T entity) {
|
||||
T newEntity = activityRepository.save(entity);
|
||||
activityNotificationService.sendCreateNotification(newEntity);
|
||||
return newEntity;
|
||||
}
|
||||
|
||||
public abstract T update(T entity);
|
||||
|
||||
public abstract D update(D entity);
|
||||
|
||||
public abstract boolean delete(Integer id);
|
||||
|
||||
public abstract T findById(Integer id);
|
||||
|
||||
public abstract PageableItems<T> findAll(int offset, int count);
|
||||
|
||||
public PageableItems<L> findAllDto(int offset, int count) {
|
||||
return convertPageable(findAll(offset, count), entity -> getActivityListDto(entity));
|
||||
}
|
||||
|
||||
protected abstract T copyFromDto(T t, D d) throws IOException;
|
||||
|
||||
protected abstract L getActivityListDto(T entity);
|
||||
|
||||
protected abstract T getNewActivity();
|
||||
|
||||
protected abstract D getNewActivityDto(T entity);
|
||||
|
||||
protected boolean checkUniqueName(String title, Integer id) {
|
||||
return title.equals(activityRepository.findByNameAndNotId(title, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void ping(int entityId) {
|
||||
pingService.addPing(findById(entityId));
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package ru.ulstu.activity.students.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.activity.common.service.ActivityNotificationService;
|
||||
import ru.ulstu.activity.students.model.Task;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
|
||||
@Service
|
||||
public class TaskNotificationService extends ActivityNotificationService<Task> {
|
||||
|
||||
private final MailService mailService;
|
||||
|
||||
public TaskNotificationService(MailService mailService) {
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
public void sendCreateNotification(Task task) {
|
||||
throw new RuntimeException("not implemented yet");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue