Bulk duplicate
This commit is contained in:
parent
449e58b8ec
commit
f6acfd83d1
@ -66,6 +66,10 @@ export class BulkService implements ISearchService {
|
||||
this.api.getReturnItem("bulk/run/" + bulk.id, _ => _, next);
|
||||
}
|
||||
|
||||
duplicate(bulk: Bulk, next: (item: Bulk) => void): void {
|
||||
this.api.getReturnItem("bulk/duplicate/" + bulk.id, _ => _, next);
|
||||
}
|
||||
|
||||
setEntry(entry: BulkEntry, key: string, value: any, next: (result: BulkEntry) => void = NO_OP): void {
|
||||
this.api.putReturnItem("BulkEntry/" + entry.id + "/set/" + key, value, next);
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
<th>Bezeichnung</th>
|
||||
<th> </th>
|
||||
<th> </th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
<tr *ngFor="let bulk of bulks.sort(Bulk.compareName); trackBy: Bulk.trackBy">
|
||||
|
||||
@ -24,6 +25,10 @@
|
||||
<fa-icon title="Ausführen" [icon]="faPlay"></fa-icon>
|
||||
</td>
|
||||
|
||||
<td class="duplicate" (click)="duplicate(bulk)">
|
||||
<fa-icon title="Duplizieren" [icon]="faCopy"></fa-icon>
|
||||
</td>
|
||||
|
||||
<td class="delete" (click)="delete(bulk)">
|
||||
<fa-icon title="Löschen" [icon]="faTimes"></fa-icon>
|
||||
</td>
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {BulkService} from "../../../api/bulk/BulkService";
|
||||
import {Bulk} from "../../../api/bulk/Bulk";
|
||||
import {faCheckCircle, faCircle, faPlayCircle, faTimesCircle} from "@fortawesome/free-regular-svg-icons";
|
||||
import {faCheckCircle, faCircle, faCopy, faPlayCircle, faTimesCircle} from "@fortawesome/free-regular-svg-icons";
|
||||
|
||||
@Component({
|
||||
selector: 'app-bulk-list',
|
||||
@ -60,10 +60,15 @@ export class BulkListComponent implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
duplicate(bulk: Bulk): void {
|
||||
this.bulkService.duplicate(bulk, bulk => this.addOrReplace(bulk));
|
||||
}
|
||||
|
||||
delete(bulk: Bulk): void {
|
||||
if (confirm("Zeitplan \"" + bulk.name + "\" wirklich löschen?")) {
|
||||
this.bulkService.delete(bulk, () => this.remove(bulk));
|
||||
}
|
||||
}
|
||||
|
||||
protected readonly faCopy = faCopy;
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
import {getBaseUrl} from "./UrlHelper";
|
||||
|
||||
const PROD: boolean = true;
|
||||
const PROD: boolean = false;
|
||||
|
||||
export const environment = {
|
||||
production: false,
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
package de.ph87.homeautomation.bulk;
|
||||
|
||||
import de.ph87.homeautomation.bulk.entry.BulkEntry;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import de.ph87.homeautomation.bulk.entry.*;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@ -16,33 +12,39 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
@SuppressWarnings("FieldMayBeFinal")
|
||||
public class Bulk {
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
|
||||
@Setter
|
||||
private boolean enabled;
|
||||
|
||||
|
||||
@Setter
|
||||
@Column(nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
|
||||
@ToString.Exclude
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<BulkEntry> entries = new ArrayList<>();
|
||||
|
||||
|
||||
@ToString.Include
|
||||
public int entryCount() {
|
||||
return entries.size();
|
||||
}
|
||||
|
||||
|
||||
public Bulk(final Bulk original, final String name) {
|
||||
this.enabled = original.isEnabled();
|
||||
this.name = name;
|
||||
this.entries = original.getEntries().stream().map(BulkEntry::new).toList();
|
||||
}
|
||||
|
||||
public Bulk(final BulkCreateDto dto) {
|
||||
this.enabled = dto.isEnabled();
|
||||
this.name = dto.getName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
package de.ph87.homeautomation.bulk;
|
||||
|
||||
import de.ph87.homeautomation.shared.ISearchController;
|
||||
import de.ph87.homeautomation.shared.SearchResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import de.ph87.homeautomation.shared.*;
|
||||
import lombok.*;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("bulk")
|
||||
@ -26,22 +25,27 @@ public class BulkController implements ISearchController {
|
||||
public Page<BulkDto> filter(@RequestBody final BulkFilter filter) {
|
||||
return bulkReader.filter(filter);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("delete/{id}")
|
||||
public void delete(@PathVariable final long id) {
|
||||
bulkWriter.delete(id);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("run/{id}")
|
||||
public void run(@PathVariable final long id) {
|
||||
bulkWriter.run(id);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("duplicate/{id}")
|
||||
public BulkDto duplicate(@PathVariable final long id) {
|
||||
return bulkWriter.duplicate(id);
|
||||
}
|
||||
|
||||
@PostMapping("set/{id}/name")
|
||||
public BulkDto name(@PathVariable final long id, @RequestBody final String name) {
|
||||
return bulkWriter.set(id, bulk -> bulk.setName(name));
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("set/{id}/enabled")
|
||||
public BulkDto enabled(@PathVariable final long id, @RequestBody final boolean enabled) {
|
||||
return bulkWriter.set(id, bulk -> bulk.setEnabled(enabled));
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package de.ph87.homeautomation.bulk;
|
||||
|
||||
import de.ph87.homeautomation.property.Property;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import de.ph87.homeautomation.property.*;
|
||||
import org.springframework.data.jpa.repository.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
public interface BulkRepository extends JpaRepository<Bulk, Long>, JpaSpecificationExecutor<Bulk> {
|
||||
|
||||
|
||||
@Deprecated(since = "Use 'filter' instead", forRemoval = true)
|
||||
List<Bulk> findAllByNameLikeIgnoreCase(String term);
|
||||
|
||||
|
||||
Optional<Bulk> findByEntries_Id(long id);
|
||||
|
||||
|
||||
List<Bulk> findDistinctByEntries_Property(Property property);
|
||||
|
||||
|
||||
boolean existsByName(String name);
|
||||
|
||||
}
|
||||
|
||||
@ -1,52 +1,65 @@
|
||||
package de.ph87.homeautomation.bulk;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import lombok.*;
|
||||
import lombok.extern.slf4j.*;
|
||||
import org.springframework.stereotype.*;
|
||||
import org.springframework.transaction.annotation.*;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class BulkWriter {
|
||||
|
||||
|
||||
private final BulkRepository bulkRepository;
|
||||
|
||||
|
||||
private final BulkReader bulkReader;
|
||||
|
||||
|
||||
private final BulkMapper bulkMapper;
|
||||
|
||||
|
||||
private final BulkExecutor bulkExecutor;
|
||||
|
||||
|
||||
public Bulk create(final BulkCreateDto dto) {
|
||||
final Bulk bulk = bulkRepository.save(new Bulk(dto));
|
||||
log.info("Bulk created: {}", bulk);
|
||||
return bulk;
|
||||
}
|
||||
|
||||
|
||||
public void delete(final long id) {
|
||||
final Bulk bulk = bulkReader.getById(id);
|
||||
bulkRepository.delete(bulk);
|
||||
log.info("Bulk deleted: {}", bulk);
|
||||
}
|
||||
|
||||
|
||||
public void run(final long id) {
|
||||
final Bulk bulk = bulkReader.getById(id);
|
||||
bulkExecutor.execute(bulk);
|
||||
}
|
||||
|
||||
|
||||
public BulkDto duplicate(final long id) {
|
||||
final Bulk original = bulkReader.getById(id);
|
||||
int number = 1;
|
||||
while (true) {
|
||||
final String copyName = original.getName() + " Kopie " + number;
|
||||
if (!bulkRepository.existsByName(copyName)) {
|
||||
final Bulk copy = bulkRepository.save(new Bulk(original, copyName));
|
||||
log.info("Bulk duplicated: {}", copy);
|
||||
return bulkMapper.toDto(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BulkDto set(final long id, final Consumer<Bulk> consumer) {
|
||||
final Bulk bulk = bulkReader.getById(id);
|
||||
consumer.accept(bulk);
|
||||
log.info("Changed Bulk: {}", bulk);
|
||||
return bulkMapper.toDto(bulk);
|
||||
}
|
||||
|
||||
|
||||
public BulkDto createDto(final BulkCreateDto dto) {
|
||||
return bulkMapper.toDto(create(dto));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
package de.ph87.homeautomation.bulk.entry;
|
||||
|
||||
import de.ph87.homeautomation.property.Property;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import de.ph87.homeautomation.property.*;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@ -13,32 +10,39 @@ import javax.persistence.*;
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class BulkEntry {
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
|
||||
@Setter
|
||||
private boolean enabled;
|
||||
|
||||
|
||||
@Setter
|
||||
private int position;
|
||||
|
||||
|
||||
@Setter
|
||||
@ManyToOne
|
||||
private Property property;
|
||||
|
||||
|
||||
@Setter
|
||||
@Column(name = "value_")
|
||||
private double value;
|
||||
|
||||
|
||||
public BulkEntry(final BulkEntryCreateDto dto, final Property property) {
|
||||
this.position = dto.getPosition();
|
||||
this.property = property;
|
||||
this.value = dto.getValue();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("CopyConstructorMissesField")
|
||||
public BulkEntry(final BulkEntry original) {
|
||||
this.position = original.getPosition();
|
||||
this.property = original.getProperty();
|
||||
this.value = original.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user