Delete slots
This commit is contained in:
		
							parent
							
								
									7dbfd77547
								
							
						
					
					
						commit
						d2d0b98083
					
				| @ -20,7 +20,6 @@ import ru.vyatsu.qr_access_admin.common.view.MainView; | |||||||
| import ru.vyatsu.qr_access_admin.door.view.DoorView; | import ru.vyatsu.qr_access_admin.door.view.DoorView; | ||||||
| import ru.vyatsu.qr_access_admin.partner.view.PartnerView; | import ru.vyatsu.qr_access_admin.partner.view.PartnerView; | ||||||
| import ru.vyatsu.qr_access_admin.qr.view.QrView; | import ru.vyatsu.qr_access_admin.qr.view.QrView; | ||||||
| import ru.vyatsu.qr_access_admin.slot.view.SlotView; |  | ||||||
| import ru.vyatsu.qr_access_admin.unit.view.UnitView; | import ru.vyatsu.qr_access_admin.unit.view.UnitView; | ||||||
| 
 | 
 | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
| @ -122,7 +121,6 @@ public class MainLayout extends AppLayout { | |||||||
|                 createTab("Устройства", UnitView.class), |                 createTab("Устройства", UnitView.class), | ||||||
|                 createTab("Двери", DoorView.class), |                 createTab("Двери", DoorView.class), | ||||||
|                 createTab("QR-коды", QrView.class), |                 createTab("QR-коды", QrView.class), | ||||||
|                 createTab("Слоты", SlotView.class), |  | ||||||
|                 createTab("Партнеры", PartnerView.class), |                 createTab("Партнеры", PartnerView.class), | ||||||
|                 createTab("Клиенты", ClientView.class) |                 createTab("Клиенты", ClientView.class) | ||||||
|         }; |         }; | ||||||
|  | |||||||
| @ -1,79 +0,0 @@ | |||||||
| package ru.vyatsu.qr_access_admin.slot.component; |  | ||||||
| 
 |  | ||||||
| import com.vaadin.flow.component.Composite; |  | ||||||
| import com.vaadin.flow.component.Key; |  | ||||||
| import com.vaadin.flow.component.button.Button; |  | ||||||
| import com.vaadin.flow.component.button.ButtonVariant; |  | ||||||
| import com.vaadin.flow.component.combobox.ComboBox; |  | ||||||
| import com.vaadin.flow.component.datetimepicker.DateTimePicker; |  | ||||||
| import com.vaadin.flow.component.icon.VaadinIcon; |  | ||||||
| import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |  | ||||||
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; |  | ||||||
| import com.vaadin.flow.component.timepicker.TimePicker; |  | ||||||
| import com.vaadin.flow.data.binder.BeanValidationBinder; |  | ||||||
| import com.vaadin.flow.data.binder.Binder; |  | ||||||
| import lombok.Getter; |  | ||||||
| import lombok.Setter; |  | ||||||
| import ru.vyatsu.qr_access_admin.door.entity.DoorEntity; |  | ||||||
| import ru.vyatsu.qr_access_admin.door.entity.DoorRepository; |  | ||||||
| import ru.vyatsu.qr_access_admin.slot.entity.SlotEntity; |  | ||||||
| 
 |  | ||||||
| import java.util.Map; |  | ||||||
| import java.util.stream.Collectors; |  | ||||||
| 
 |  | ||||||
| public class SlotEditor extends Composite<VerticalLayout> { |  | ||||||
| 
 |  | ||||||
|     public interface SaveListener { |  | ||||||
|         void onSave(SlotEntity qr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public interface DeleteListener { |  | ||||||
|         void onDelete(SlotEntity qr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public interface CancelListener { |  | ||||||
|         void onCancel(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private volatile SlotEntity currentSlot; |  | ||||||
| 
 |  | ||||||
|     @Getter |  | ||||||
|     @Setter |  | ||||||
|     private SaveListener saveListener; |  | ||||||
|     @Getter |  | ||||||
|     @Setter |  | ||||||
|     private DeleteListener deleteListener; |  | ||||||
|     @Getter |  | ||||||
|     @Setter |  | ||||||
|     private CancelListener cancelListener; |  | ||||||
| 
 |  | ||||||
|     private final Binder<SlotEntity> binder = new BeanValidationBinder<>(SlotEntity.class); |  | ||||||
| 
 |  | ||||||
|     public void setCurrentSlot(SlotEntity slot) { |  | ||||||
|         this.currentSlot = slot; |  | ||||||
|         binder.setBean(slot); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public SlotEditor() { |  | ||||||
|         var startTime = new TimePicker("Начало действия"); |  | ||||||
|         var endTime = new TimePicker("Конец действия"); |  | ||||||
| 
 |  | ||||||
|         var save = new Button("Сохранить", VaadinIcon.CHECK.create()); |  | ||||||
|         var cancel = new Button("Отмена"); |  | ||||||
|         var delete = new Button("Удалить", VaadinIcon.TRASH.create()); |  | ||||||
| 
 |  | ||||||
|         binder.forField(startTime).bind("startTime"); |  | ||||||
|         binder.forField(endTime).bind("endTime"); |  | ||||||
| 
 |  | ||||||
|         save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); |  | ||||||
|         save.addClickListener(e -> saveListener.onSave(currentSlot)); |  | ||||||
|         save.addClickShortcut(Key.ENTER); |  | ||||||
| 
 |  | ||||||
|         delete.addThemeVariants(ButtonVariant.LUMO_ERROR); |  | ||||||
|         delete.addClickListener(e -> deleteListener.onDelete(currentSlot)); |  | ||||||
| 
 |  | ||||||
|         cancel.addClickListener(e -> cancelListener.onCancel()); |  | ||||||
| 
 |  | ||||||
|         getContent().add(startTime, endTime, new HorizontalLayout(save, cancel, delete)); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,24 +0,0 @@ | |||||||
| package ru.vyatsu.qr_access_admin.slot.entity; |  | ||||||
| 
 |  | ||||||
| import jakarta.persistence.*; |  | ||||||
| import lombok.Getter; |  | ||||||
| import lombok.Setter; |  | ||||||
| 
 |  | ||||||
| import java.time.LocalTime; |  | ||||||
| 
 |  | ||||||
| @Entity |  | ||||||
| @Table(name = "slots") |  | ||||||
| @Getter |  | ||||||
| @Setter |  | ||||||
| public class SlotEntity { |  | ||||||
|     @Id |  | ||||||
|     @Column |  | ||||||
|     @GeneratedValue(strategy = GenerationType.UUID) |  | ||||||
|     private String id; |  | ||||||
| 
 |  | ||||||
|     @Column |  | ||||||
|     private LocalTime startTime; |  | ||||||
| 
 |  | ||||||
|     @Column |  | ||||||
|     private LocalTime endTime; |  | ||||||
| } |  | ||||||
| @ -1,8 +0,0 @@ | |||||||
| package ru.vyatsu.qr_access_admin.slot.entity; |  | ||||||
| 
 |  | ||||||
| import org.springframework.data.jpa.repository.JpaRepository; |  | ||||||
| import org.springframework.stereotype.Repository; |  | ||||||
| 
 |  | ||||||
| @Repository |  | ||||||
| public interface SlotRepository extends JpaRepository<SlotEntity, String> { |  | ||||||
| } |  | ||||||
| @ -1,78 +0,0 @@ | |||||||
| package ru.vyatsu.qr_access_admin.slot.view; |  | ||||||
| 
 |  | ||||||
| import com.vaadin.flow.component.button.Button; |  | ||||||
| import com.vaadin.flow.component.grid.Grid; |  | ||||||
| import com.vaadin.flow.component.icon.VaadinIcon; |  | ||||||
| import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |  | ||||||
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; |  | ||||||
| import com.vaadin.flow.router.PageTitle; |  | ||||||
| import com.vaadin.flow.router.Route; |  | ||||||
| import jakarta.annotation.security.PermitAll; |  | ||||||
| import ru.vyatsu.qr_access_admin.common.MainLayout; |  | ||||||
| import ru.vyatsu.qr_access_admin.slot.component.SlotEditor; |  | ||||||
| import ru.vyatsu.qr_access_admin.slot.entity.SlotEntity; |  | ||||||
| import ru.vyatsu.qr_access_admin.slot.entity.SlotRepository; |  | ||||||
| 
 |  | ||||||
| import java.util.List; |  | ||||||
| 
 |  | ||||||
| @Route(value = "slots", layout = MainLayout.class) |  | ||||||
| @PageTitle("Слоты") |  | ||||||
| @PermitAll |  | ||||||
| public class SlotView extends VerticalLayout { |  | ||||||
|     private final SlotRepository repository; |  | ||||||
|     private final Grid<SlotEntity> grid; |  | ||||||
|     private final SlotEditor editor; |  | ||||||
| 
 |  | ||||||
|     public SlotView(SlotRepository repository) { |  | ||||||
|         this.repository = repository; |  | ||||||
| 
 |  | ||||||
|         var addButton = new Button("Добавить слот", VaadinIcon.PLUS.create()); |  | ||||||
|         grid = new Grid<>(SlotEntity.class); |  | ||||||
|         grid.setColumns("id", "startTime", "endTime"); |  | ||||||
|         editor = new SlotEditor(); |  | ||||||
| 
 |  | ||||||
|         var actionsLayout = new HorizontalLayout(addButton); |  | ||||||
|         add(actionsLayout, grid, editor); |  | ||||||
| 
 |  | ||||||
|         this.configureEditor(); |  | ||||||
| 
 |  | ||||||
|         addButton.addClickListener(e -> editSlot(new SlotEntity())); |  | ||||||
| 
 |  | ||||||
|         grid.setHeight("200px"); |  | ||||||
|         grid.asSingleSelect().addValueChangeListener(e -> editSlot(e.getValue())); |  | ||||||
| 
 |  | ||||||
|         refreshSlotGrid(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private void refreshSlotGrid() { |  | ||||||
|         List<SlotEntity> entities = repository.findAll(); |  | ||||||
|         grid.setItems(entities); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private void editSlot(SlotEntity slot) { |  | ||||||
|         if (slot == null) { |  | ||||||
|             editor.setVisible(false); |  | ||||||
|         } else { |  | ||||||
|             editor.setVisible(true); |  | ||||||
|             editor.setCurrentSlot(slot); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private void configureEditor() { |  | ||||||
|         editor.setVisible(false); |  | ||||||
| 
 |  | ||||||
|         editor.setSaveListener(slot -> { |  | ||||||
|             repository.save(slot); |  | ||||||
|             refreshSlotGrid(); |  | ||||||
|             editSlot(null); |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         editor.setDeleteListener(slot -> { |  | ||||||
|             repository.deleteById(slot.getId()); |  | ||||||
|             refreshSlotGrid(); |  | ||||||
|             editSlot(null); |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         editor.setCancelListener(() -> editSlot(null)); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user