Adding edit views

This commit is contained in:
alex 2024-08-06 08:11:28 -04:00
parent 4fb047f9e5
commit e3a071a7f1
11 changed files with 96 additions and 33 deletions

View File

@ -6,6 +6,7 @@ import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -15,16 +16,16 @@ import java.util.Optional;
@EqualsAndHashCode(callSuper = true)
public class Assessment extends BaseEntity {
@OneToMany(fetch = FetchType.EAGER)
private List<Question> questions;
private List<Question> questions = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "assessment", cascade = {CascadeType.ALL})
private List<Submission> submissions;
private List<Submission> submissions = new ArrayList<>();
@ManyToOne
private AppUser appUser;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "assessment", cascade = {CascadeType.ALL})
private List<AssessmentEvent> assessmentEvents = List.of();
private List<AssessmentEvent> assessmentEvents = new ArrayList<>();
public Submission getCurrentSubmission() {
return submissions.getLast();

View File

@ -12,5 +12,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Question extends BaseEntity{
private String title;
private String description;
private String content;
}

View File

@ -0,0 +1,9 @@
package com.primefactorsolutions.repositories;
import com.primefactorsolutions.model.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;
public interface QuestionRepository extends JpaRepository<Question, UUID> {
}

View File

@ -145,4 +145,8 @@ public class AssessmentService {
submission.setResponse(currSubmission.getResponse());
Assessment saved = assessmentRepository.save(assessment);
}
public Assessment saveAssessment(final Assessment assessment) {
return assessmentRepository.save(assessment);
}
}

View File

@ -0,0 +1,18 @@
package com.primefactorsolutions.service;
import com.primefactorsolutions.model.Question;
import com.primefactorsolutions.repositories.QuestionRepository;
import lombok.Data;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Data
public class QuestionService {
private final QuestionRepository questionRepository;
public List<Question> getQuestions() {
return questionRepository.findAll();
}
}

View File

@ -1,9 +1,15 @@
package com.primefactorsolutions.views;
import com.primefactorsolutions.model.AppUser;
import com.primefactorsolutions.model.Assessment;
import com.primefactorsolutions.model.Question;
import com.primefactorsolutions.service.AssessmentService;
import com.primefactorsolutions.service.QuestionService;
import com.primefactorsolutions.service.UserService;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.ItemLabelGenerator;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.PageTitle;
@ -11,6 +17,7 @@ import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.fields.SubListSelector;
import org.vaadin.firitin.form.BeanValidationForm;
import java.util.List;
@ -20,32 +27,43 @@ import java.util.UUID;
@Scope("prototype")
@PageTitle("Assessments")
@Route(value = "/assessments", layout = MainLayout.class)
@Uses(ComboBox.class)
public class AssessmentView extends BeanValidationForm<Assessment> implements HasUrlParameter<String> {
private final AssessmentService assessmentService;
private final TextField appUserName = new TextField();
private ComboBox<AppUser> appUser = null;
private SubListSelector<Question> questions = null;
public AssessmentView(AssessmentService assessmentService) {
public AssessmentView(AssessmentService assessmentService, QuestionService questionService, UserService userService) {
super(Assessment.class);
setEntityWithEnabledSave(new Assessment());
this.assessmentService = assessmentService;
setSavedHandler((SavedHandler<Assessment>) assessment -> {
System.out.println(assessment);
});
}
appUser = new ComboBox<>("AppUser", userService.getUsers());
appUser.setItemLabelGenerator((ItemLabelGenerator<AppUser>) AppUser::getEmail);
appUser.setReadOnly(false);
questions = new SubListSelector<>(Question.class);
questions.setItemLabelGenerator((ItemLabelGenerator<Question>) Question::getTitle);
questions.setReadOnly(false);
questions.setAvailableOptions(questionService.getQuestions());
setSavedHandler((SavedHandler<Assessment>) this.assessmentService::saveAssessment);
}
@Override
public void setParameter(final BeforeEvent beforeEvent, final String s) {
if (StringUtils.isNotBlank(s) && !"new".equals(s)) {
var assessment = assessmentService.getAssessment(UUID.fromString(s));
setEntityWithEnabledSave(assessment);
} else {
setEntityWithEnabledSave(new Assessment());
}
}
@Override
protected List<Component> getFormComponents() {
return List.of(appUserName);
return List.of(appUser, questions);
}
}

View File

@ -23,7 +23,9 @@ public class AssessmentsListView extends Main {
public AssessmentsListView(final AssessmentService assessmentService) {
final VGrid<Assessment> grid = new VGrid<>(Assessment.class);
grid.setDataProvider(new DataProvider<Assessment, Object>() {
grid.setColumns("id", "appUser.email");
grid.setDataProvider(new DataProvider<>() {
@Override
public boolean isInMemory() {
return false;
@ -31,12 +33,16 @@ public class AssessmentsListView extends Main {
@Override
public int size(Query<Assessment, Object> query) {
return 0;
return assessmentService.getAssessments().size();
}
@Override
public Stream<Assessment> fetch(Query<Assessment, Object> query) {
return null;
int limit = query.getLimit();
int pagerSize = query.getPageSize();
int page = query.getPage();
return assessmentService.getAssessments().stream();
}
@Override

View File

@ -23,29 +23,30 @@ import java.util.UUID;
public class UserView extends BeanValidationForm<AppUser> implements HasUrlParameter<String> {
private final UserService userService;
private final TextField appUserName = new TextField();
private TextField email = null;
public UserView(UserService userService) {
public UserView(final UserService userService) {
super(AppUser.class);
setEntityWithEnabledSave(new AppUser("foo@bar.com", List.of()));
this.userService = userService;
email = new TextField();
email.setLabel("Email");
setSavedHandler((SavedHandler<AppUser>) appUser -> {
System.out.println(appUser);
});
setSavedHandler((SavedHandler<AppUser>) userService::createOrUpdate);
}
@Override
public void setParameter(final BeforeEvent beforeEvent, final String s) {
if (StringUtils.isNotBlank(s) && !"new".equals(s)) {
var assessment = userService.getUser(UUID.fromString(s));
var user = userService.getUser(UUID.fromString(s));
setEntityWithEnabledSave(user);
} else {
setEntityWithEnabledSave(new AppUser());
}
}
@Override
protected List<Component> getFormComponents() {
return List.of(appUserName);
return List.of(email);
}
}

View File

@ -1,6 +1,7 @@
package com.primefactorsolutions.views;
import com.primefactorsolutions.model.AppUser;
import com.primefactorsolutions.service.UserService;
import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.DataProviderListener;
@ -20,11 +21,14 @@ import java.util.stream.Stream;
@PageTitle("Users")
@Route(value = "/users", layout = MainLayout.class)
public class UsersListView extends Main {
final UserService userService;
public UsersListView() {
public UsersListView(final UserService userService) {
this.userService = userService;
final VGrid<AppUser> grid = new VGrid<>(AppUser.class);
grid.setColumns("id", "email");
grid.setDataProvider(new DataProvider<AppUser, Object>() {
grid.setDataProvider(new DataProvider<>() {
@Override
public boolean isInMemory() {
return false;
@ -32,7 +36,7 @@ public class UsersListView extends Main {
@Override
public int size(Query<AppUser, Object> query) {
return 1;
return userService.getUsers().size();
}
@Override
@ -40,7 +44,7 @@ public class UsersListView extends Main {
int limit = query.getLimit();
int pagerSize = query.getPageSize();
int page = query.getPage();
return Stream.of(new AppUser("foo@bar.com", List.of()));
return userService.getUsers().stream();
}
@Override

View File

@ -6,7 +6,7 @@ spring.mustache.check-template-location = false
vaadin.launch-browser=true
# To improve the performance during development.
# For more information https://vaadin.com/docs/latest/integrations/spring/configuration#special-configuration-parameters
vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin
vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin,org.vaadin.firitin
# spring.datasource.url=jdbc:h2:mem:testdb

View File

@ -1,7 +1,7 @@
insert into app_user (id, version, email) values ('23471ab3-f639-4d2b-9541-7227f4ea7ee6', 1, 'foo@bar.com');
insert into question (id, version, content) values ('a7e00ff8-da41-4624-b31c-1b13c3f2e3ae', 1, 'foo bar');
insert into question (id, version, content) values ('8a4b213c-ca81-4c38-b56d-d7028c2dde88', 1, 'foo buzz');
insert into question (id, version, content, title, description) values ('a7e00ff8-da41-4624-b31c-1b13c3f2e3ae', 1, 'foo bar', 'q1', 'lorem ipsum');
insert into question (id, version, content, title, description) values ('8a4b213c-ca81-4c38-b56d-d7028c2dde88', 1, 'foo buzz', 'q2', 'lorem ipsum');
insert into assessment(id, version, app_user_id) values ('46b153f4-23fd-462f-8430-fbe67b83caab', 1, '23471ab3-f639-4d2b-9541-7227f4ea7ee6');