Compare commits

...

1 Commits

Author SHA1 Message Date
e02fd21d70 complete lab 2023-05-16 13:53:19 +04:00
33 changed files with 296 additions and 398 deletions

11
pom.xml
View File

@ -11,6 +11,7 @@
<groupId>com.example</groupId>
<artifactId>sybd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>sybd</name>
<description>sybd</description>
<properties>
@ -21,11 +22,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -57,7 +57,6 @@
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.datafaker/datafaker -->
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>

View File

@ -1,8 +1,13 @@
package com.example.sybd.Dto;
import com.example.sybd.models.Client;
import com.example.sybd.models.Visit;
import com.example.sybd.services.VisitService;
import lombok.Data;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
@Data
public class ClientDto {
@ -12,5 +17,22 @@ public class ClientDto {
private String secondName;
private String telephone;
private String address;
private List<Long> visits;
private List<Visit> visits;
public static ClientDto create(Client client, VisitService visitService) {
ClientDto clientDto = new ClientDto();
clientDto.setId(client.getClient_id());
clientDto.setName(client.getName());
clientDto.setSecondName(client.getSecondName());
clientDto.setLastName(client.getLastName());
clientDto.setAddress(client.getAddress());
clientDto.setTelephone(client.getTelephone());
clientDto.visits = visitService.all()
.stream().filter(e -> Objects.equals(e.getClient_id(), clientDto.id)).toList();
return clientDto;
}
public String getFio() {
return MessageFormat.format("{0} {1} {2}", secondName, name, lastName);
}
}

View File

@ -1,9 +1,15 @@
package com.example.sybd.Dto;
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
public class IdNameDto {
private Long id;
private String name;
public static IdNameDto of(Long id, String name) {
return new IdNameDto(id, name);
}
}

View File

@ -1,10 +1,26 @@
package com.example.sybd.Dto;
import com.example.sybd.models.Post;
import com.example.sybd.services.GroupServiceService;
import lombok.Data;
import java.util.Objects;
@Data
public class PostDto {
private Long id;
private String name;
private IdNameDto group;
private ServiceGroupDto group;
public static PostDto create(Post post, GroupServiceService groupServiceService) {
PostDto postDto = new PostDto();
postDto.setId(post.getPost_id());
postDto.setName(post.getName());
postDto.setGroup(
ServiceGroupDto.create(
Objects.requireNonNull(groupServiceService.get(post.getGroup_id()))
)
);
return postDto;
}
}

View File

@ -1,11 +1,29 @@
package com.example.sybd.Dto;
import com.example.sybd.models.Post;
import com.example.sybd.models.Service;
import com.example.sybd.services.GroupServiceService;
import lombok.Data;
import java.util.Objects;
@Data
public class ServiceDto {
private Long id;
private String name;
private IdNameDto group;
private ServiceGroupDto group;
private Integer cost;
public static ServiceDto create(Service service, GroupServiceService groupServiceService) {
ServiceDto serviceDto = new ServiceDto();
serviceDto.setId(service.getServices_id());
serviceDto.setName(service.getName());
serviceDto.setCost(service.getCost());
serviceDto.setGroup(
ServiceGroupDto.create(
Objects.requireNonNull(groupServiceService.get(service.getGroup_id()))
)
);
return serviceDto;
}
}

View File

@ -1,9 +1,17 @@
package com.example.sybd.Dto;
import com.example.sybd.models.ServiceGroup;
import lombok.Data;
@Data
public class ServiceGroupDto {
private Long id;
private String name;
public static ServiceGroupDto create(ServiceGroup serviceGroup) {
ServiceGroupDto serviceGroupDto = new ServiceGroupDto();
serviceGroupDto.setId(serviceGroup.getServicesGroup_id());
serviceGroupDto.setName(serviceGroup.getName());
return serviceGroupDto;
}
}

View File

@ -1,5 +1,13 @@
package com.example.sybd.Dto;
import com.example.sybd.models.Client;
import com.example.sybd.models.Service;
import com.example.sybd.models.Visit;
import com.example.sybd.models.Worker;
import com.example.sybd.services.ClientService;
import com.example.sybd.services.GroupServiceService;
import com.example.sybd.services.ServicesService;
import com.example.sybd.services.WorkerService;
import lombok.Data;
import java.sql.Date;
@ -14,4 +22,36 @@ public class VisitDto {
private Date date;
private Time time;
private boolean isEnded;
private Integer cost;
public static VisitDto create(Visit visit,
ClientService clientService,
WorkerService workerService,
ServicesService servicesService,
GroupServiceService groupServiceService) {
VisitDto visitDto = new VisitDto();
visitDto.setId(visit.getVisit_id());
Client client = clientService.get(visit.getClient_id());
if (client == null) return null;
visitDto.setClient(
IdNameDto.of(client.getClient_id(), client.getFio())
);
Worker worker = workerService.get(visit.getWorker_id());
if (worker == null) return null;
visitDto.setWorker(
IdNameDto.of(worker.getWorker_id(), worker.getFio())
);
Service service = servicesService.get(visit.getService_id());
if (service == null) return null;
visitDto.setService(
ServiceDto.create(service, groupServiceService)
);
visitDto.setEnded(visit.isEnded());
visitDto.setDate(visit.getDateVisit());
visitDto.setTime(visit.getTime());
visitDto.setCost(
service.getCost()
);
return visitDto;
}
}

View File

@ -1,8 +1,16 @@
package com.example.sybd.Dto;
import com.example.sybd.models.Visit;
import com.example.sybd.models.Worker;
import com.example.sybd.services.GroupServiceService;
import com.example.sybd.services.PostService;
import com.example.sybd.services.ServicesService;
import com.example.sybd.services.VisitService;
import lombok.Data;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
@Data
public class WorkerDto {
@ -13,5 +21,36 @@ public class WorkerDto {
private String telephone;
private String address;
private PostDto post;
private List<Long> visits;
private List<Visit> visits;
private Integer money;
public static WorkerDto create(Worker worker,
VisitService visitService,
PostService postService,
ServicesService servicesService,
GroupServiceService groupServiceService) {
WorkerDto workerDto = new WorkerDto();
workerDto.setId(worker.getWorker_id());
workerDto.setName(worker.getName());
workerDto.setSecondName(worker.getSecondName());
workerDto.setLastName(worker.getLastName());
workerDto.setAddress(worker.getAddress());
workerDto.setTelephone(worker.getTelephone());
workerDto.visits = visitService.all()
.stream().filter(e -> Objects.equals(e.getWorker_id(), workerDto.id)).toList();
workerDto.money = servicesService.all()
.stream().filter(e -> workerDto.visits.stream().map(Visit::getService_id).toList().contains(e.getServices_id()))
.reduce(0, (cost, e) -> cost + e.getCost(), Integer::sum);
workerDto.setPost(
PostDto.create(
Objects.requireNonNull(postService.get(worker.getPost_id())),
groupServiceService
)
);
return workerDto;
}
public String getFio() {
return MessageFormat.format("{0} {1} {2}", secondName, name, lastName);
}
}

View File

@ -10,45 +10,45 @@ import java.time.LocalTime;
import java.util.List;
public class random {
private static final Faker faker = new Faker();
public static Client getPerson() {
String tel = faker.phoneNumber()
.phoneNumberInternational()
.replace("-", "")
.replace(" ", "")
.substring(0, 11);
String address = faker.address().streetAddress();
Client client = new Client();
client.setName(faker.name().firstName());
client.setSecondName(faker.name().lastName());
client.setLastName("-");
client.setTelephone(tel);
client.setAddress(address);
return client;
}
public static Integer getNumber(Integer min, Integer max) {
return faker.number().numberBetween(min, max);
}
public static Visit getVisit(List<Client> clientList,
List<Worker> workerList,
List<Service> serviceList) {
Service service = serviceList.get(faker.number().numberBetween(0, serviceList.size()));
var workers = workerList.stream()
.filter((elem) -> elem.getPost().getGroup() == service.getGroup()).toList();
Worker worker = workers.get(faker.number().numberBetween(0, workers.size() - 1));
Client client = clientList.get(faker.number().numberBetween(0, clientList.size()));
Visit visit = new Visit();
visit.setClient(client);
visit.setService(service);
visit.setWorker(worker);
visit.setDateVisit(Date.valueOf(LocalDate.now().minusDays(faker.number().numberBetween(0, 30))));
visit.setTime(Time.valueOf(LocalTime.now()
.minusHours(faker.number().numberBetween(0, 6))
.minusMinutes(faker.number().numberBetween(0, 5) * 10L)
));
return visit;
}
// private static final Faker faker = new Faker();
//
// public static Client getPerson() {
// String tel = faker.phoneNumber()
// .phoneNumberInternational()
// .replace("-", "")
// .replace(" ", "")
// .substring(0, 11);
// String address = faker.address().streetAddress();
// Client client = new Client();
// client.setName(faker.name().firstName());
// client.setSecondName(faker.name().lastName());
// client.setLastName("-");
// client.setTelephone(tel);
// client.setAddress(address);
// return client;
// }
//
// public static Integer getNumber(Integer min, Integer max) {
// return faker.number().numberBetween(min, max);
// }
//
// public static Visit getVisit(List<Client> clientList,
// List<Worker> workerList,
// List<Service> serviceList) {
// Service service = serviceList.get(faker.number().numberBetween(0, serviceList.size()));
// var workers = workerList.stream()
// .filter((elem) -> elem.getPost().getGroup() == service.getGroup()).toList();
// Worker worker = workers.get(faker.number().numberBetween(0, workers.size() - 1));
// Client client = clientList.get(faker.number().numberBetween(0, clientList.size()));
// Visit visit = new Visit();
// visit.setClient(client);
// visit.setService(service);
// visit.setWorker(worker);
// visit.setDateVisit(Date.valueOf(LocalDate.now().minusDays(faker.number().numberBetween(0, 30))));
// visit.setTime(Time.valueOf(LocalTime.now()
// .minusHours(faker.number().numberBetween(0, 6))
// .minusMinutes(faker.number().numberBetween(0, 5) * 10L)
// ));
// return visit;
// }
}

View File

@ -1,5 +1,6 @@
package com.example.sybd.controllers;
import com.example.sybd.Dto.*;
import com.example.sybd.services.*;
import com.example.sybd.utils.mappingUtils;
import org.springframework.stereotype.Controller;
@ -34,15 +35,19 @@ public class WorkerController {
@GetMapping("/")
public String main(Model model) {
model.addAttribute("visits", visitService.all());
model.addAttribute("visits", visitService.all()
.stream().map(e -> VisitDto.create(e, clientService, workerService, servicesService, groupServiceService)).toList());
return "workerMain";
}
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("clients", clientService.all());
model.addAttribute("workers", workerService.all());
model.addAttribute("services", servicesService.all());
model.addAttribute("clients", clientService.all()
.stream().map(e -> ClientDto.create(e, visitService)).toList());
model.addAttribute("workers", workerService.all()
.stream().map(e -> WorkerDto.create(e, visitService, postService, servicesService, groupServiceService)).toList());
model.addAttribute("services", servicesService.all()
.stream().map(e -> ServiceDto.create(e, groupServiceService)).toList());
return "workerCreate";
}
@ -53,9 +58,12 @@ public class WorkerController {
String date,
String time,
Model model) {
model.addAttribute("visits", visitService.all());
model.addAttribute("workers", workerService.all());
model.addAttribute("services", servicesService.all());
model.addAttribute("visits", visitService.all()
.stream().map(e -> VisitDto.create(e, clientService, workerService, servicesService, groupServiceService)).toList());
model.addAttribute("workers", workerService.all()
.stream().map(e -> WorkerDto.create(e, visitService, postService, servicesService, groupServiceService)).toList());
model.addAttribute("services", servicesService.all()
.stream().map(e -> ServiceDto.create(e, groupServiceService)).toList());
visitService.create(
client,
service,
@ -69,7 +77,8 @@ public class WorkerController {
@GetMapping("/service")
public String services(Model model) {
model.addAttribute("services", servicesService.all());
model.addAttribute("services", servicesService.all()
.stream().map(e -> ServiceDto.create(e, groupServiceService)).toList());
return "workerServices";
}
@ -80,25 +89,29 @@ public class WorkerController {
@GetMapping("/serviceGroup")
public String serviceGroups(Model model) {
model.addAttribute("serviceGroups", groupServiceService.all());
model.addAttribute("serviceGroups", groupServiceService.all()
.stream().map(ServiceGroupDto::create).toList());
return "workerServiceGroup";
}
@GetMapping("/users")
public String users(Model model) {
model.addAttribute("users", clientService.all());
model.addAttribute("users", clientService.all()
.stream().map(e -> ClientDto.create(e, visitService)).toList());
return "workerUsers";
}
@GetMapping("/workers")
public String workers(Model model) {
model.addAttribute("workers", workerService.all());
model.addAttribute("workers", workerService.all()
.stream().map(e -> WorkerDto.create(e, visitService, postService, servicesService, groupServiceService)).toList());
return "workers";
}
@GetMapping("/posts")
public String posts(Model model) {
model.addAttribute("posts", postService.all());
model.addAttribute("posts", postService.all()
.stream().map(e -> PostDto.create(e, groupServiceService)).toList());
return "workerPost";
}
}

View File

@ -1,69 +0,0 @@
package com.example.sybd.controllers;
import com.example.sybd.Random.random;
import com.example.sybd.models.Client;
import com.example.sybd.models.Visit;
import com.example.sybd.services.ClientService;
import com.example.sybd.services.ServicesService;
import com.example.sybd.services.VisitService;
import com.example.sybd.services.WorkerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/g")
@RestController
public class genController {
private final ClientService clientService;
private final WorkerService workerService;
private final ServicesService servicesService;
private final VisitService visitService;
public genController(ClientService clientService,
WorkerService workerService,
ServicesService servicesService,
VisitService visitService) {
this.clientService = clientService;
this.workerService = workerService;
this.servicesService = servicesService;
this.visitService = visitService;
}
@GetMapping("/")
public void generateBd() {
for (int i = 0; i < 10; i++) {
Client client = random.getPerson();
clientService.create(
client.getName(),
client.getSecondName(),
client.getLastName(),
client.getTelephone(),
client.getAddress()
);
Client worker = random.getPerson();
workerService.create(
worker.getName(),
worker.getSecondName(),
worker.getLastName(),
worker.getTelephone(),
worker.getAddress(),
Long.valueOf(random.getNumber(1, 8))
);
}
for (int i = 0; i < 100; i++) {
Visit visit = random.getVisit(
clientService.all(),
workerService.all(),
servicesService.all()
);
visitService.create(
visit.getClient().getId(),
visit.getService().getId(),
visit.getWorker().getId(),
visit.getDateVisit(),
visit.getTime(),
random.getNumber(0, 3) == 2
);
}
}
}

View File

@ -1,32 +1,24 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.mapping.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.text.MessageFormat;
import java.util.Collection;
@Getter
@Setter
@Entity
@Table( name = "client" )
@Document(collation = "client")
public class Client {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private Long client_id;
private String name;
private String lastName;
private String secondName;
private String telephone;
private String address;
@OneToMany( mappedBy = "client" )
private Collection<Visit> visits;
public String getFio() {
return MessageFormat.format("{0} {1} {2}", secondName, name, lastName);
}

View File

@ -1,21 +1,16 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Getter
@Setter
@Entity
@Table( name = "post" )
@Document(collation = "post")
public class Post {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private Long post_id;
private String name;
@ManyToOne
@JoinColumn( name = "group_id" )
private ServiceGroup group;
private Long group_id;
}

View File

@ -1,32 +1,17 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.util.Collection;
import org.springframework.data.mongodb.core.mapping.Document;
@Getter
@Setter
@Entity
@Table( name = "services" )
@Document( collation = "services" )
public class Service {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private Long services_id;
private String name;
@ManyToOne
@JoinColumn( name = "group_id" )
private ServiceGroup group;
private Long group_id;
private Integer cost;
@OneToMany( mappedBy = "service" )
private Collection<Visit> visit;
public void setVisit(Collection<Visit> visit) {
this.visit = visit;
}
public String getInfo() {
return String.format("%s %d$", name, cost);

View File

@ -1,22 +1,17 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table( name = "services_group" )
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
@Document(collation = "services_group")
public class ServiceGroup {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
@Column( name = "name" )
private Long servicesGroup_id;
private String name;
}

View File

@ -1,34 +1,22 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.sql.Date;
import java.sql.Time;
@Getter
@Setter
@Entity
@Table( name = "visit" )
@Document(collation = "visit")
public class Visit {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
@ManyToOne
@JoinColumn( name = "client_id" )
private Client client;
@ManyToOne
@JoinColumn( name = "service_id" )
private Service service;
@ManyToOne
@JoinColumn( name = "worker_id" )
private Worker worker;
private Long visit_id;
private Long client_id;
private Long service_id;
private Long worker_id;
private Date dateVisit;
private Time time;
private boolean isEnded;

View File

@ -1,42 +1,28 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.text.MessageFormat;
import java.util.Collection;
@Getter
@Setter
@Entity
@Table( name = "worker" )
@Document(collation = "worker")
public class Worker {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private Long worker_id;
private String name;
private String lastName;
private String secondName;
private String telephone;
private String address;
@ManyToOne
@JoinColumn( name = "post_id" )
private Post post;
@OneToMany( mappedBy = "worker" )
private Long post_id;
private Collection<Visit> visits;
public String getFio() {
return MessageFormat.format("{0} {1} {2}", secondName, name, lastName);
}
public Integer getMoney() {
return visits
.stream()
.filter(Visit::isEnded)
.reduce(0, (cost, element) -> cost + element.getService().getCost(), Integer::sum);
}
}

View File

@ -1,7 +1,7 @@
package com.example.sybd.repository;
import com.example.sybd.models.Client;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ClientRepository extends CrudRepository<Client, Long> {
public interface ClientRepository extends MongoRepository<Client, Long> {
}

View File

@ -1,7 +1,8 @@
package com.example.sybd.repository;
import com.example.sybd.models.Post;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.CrudRepository;
public interface PostRepository extends CrudRepository<Post, Long> {
public interface PostRepository extends MongoRepository<Post, Long> {
}

View File

@ -1,9 +1,10 @@
package com.example.sybd.repository;
import com.example.sybd.models.ServiceGroup;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ServiceGroupRepository extends CrudRepository<ServiceGroup, Long> {
public interface ServiceGroupRepository extends MongoRepository<ServiceGroup, Long> {
}

View File

@ -1,10 +1,9 @@
package com.example.sybd.repository;
import com.example.sybd.models.Service;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ServiceRepository extends CrudRepository<Service, Long> {
public interface ServiceRepository extends MongoRepository<Service, Long> {
}

View File

@ -1,7 +1,8 @@
package com.example.sybd.repository;
import com.example.sybd.models.Visit;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.CrudRepository;
public interface VisitRepository extends CrudRepository<Visit, Long> {
public interface VisitRepository extends MongoRepository<Visit, Long> {
}

View File

@ -1,11 +1,12 @@
package com.example.sybd.repository;
import com.example.sybd.models.Worker;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional;
public interface WorkerRepository extends CrudRepository<Worker, Long> {
public interface WorkerRepository extends MongoRepository<Worker, Long> {
List<Worker> findAllByPostId(Long id);
}

View File

@ -52,7 +52,7 @@ public class ClientService implements IService<Client, Long> {
@Override
public @Nullable Client update(Client other) {
Client client = clientRepository.findById(other.getId()).orElse(null);
Client client = clientRepository.findById(other.getClient_id()).orElse(null);
if (client == null) return null;
client.setName(other.getName());

View File

@ -44,7 +44,7 @@ public class GroupServiceService implements IService<ServiceGroup, Long> {
@Override
public @Nullable ServiceGroup update(ServiceGroup other) {
ServiceGroup serviceGroup = serviceGroupRepository.findById(other.getId()).orElse(null);
ServiceGroup serviceGroup = serviceGroupRepository.findById(other.getServicesGroup_id()).orElse(null);
if (serviceGroup == null)
return null;

View File

@ -13,22 +13,15 @@ import java.util.List;
@Service
public class PostService implements IService<Post, Long> {
private final PostRepository postRepository;
private final GroupServiceService groupServiceService;
public PostService(PostRepository postRepository, GroupServiceService groupServiceService) {
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
this.groupServiceService = groupServiceService;
}
public Post create(@NotNull String name, @NotNull Long group_id) {
ServiceGroup serviceGroup = groupServiceService.get(group_id);
if (serviceGroup == null)
return null;
Post out = new Post();
out.setName(name);
out.setGroup(serviceGroup);
out.setGroup_id(group_id);
return postRepository.save(out);
}
@ -52,13 +45,13 @@ public class PostService implements IService<Post, Long> {
@Override
public @Nullable Post update(Post other) {
Post post = postRepository.findById(other.getId()).orElse(null);
Post post = postRepository.findById(other.getPost_id()).orElse(null);
if (post == null) return null;
if (other.getName() != null)
post.setName(other.getName());
if (other.getGroup() != null)
post.setGroup(other.getGroup());
if (other.getGroup_id() != null)
post.setGroup_id(other.getGroup_id());
return postRepository.save(post);
}

View File

@ -12,23 +12,16 @@ import java.util.List;
@org.springframework.stereotype.Service
public class ServicesService implements IService<Service, Long> {
private final ServiceRepository serviceRepository;
private final GroupServiceService groupServiceService;
public ServicesService(ServiceRepository serviceRepository, GroupServiceService groupServiceService) {
public ServicesService(ServiceRepository serviceRepository) {
this.serviceRepository = serviceRepository;
this.groupServiceService = groupServiceService;
}
public Service create(@NotNull String name, @NotNull Integer cost, @NotNull Long group_id) {
ServiceGroup serviceGroup = groupServiceService.get(group_id);
if (serviceGroup == null)
return null;
Service service = new Service();
service.setName(name);
service.setCost(cost);
service.setGroup(serviceGroup);
service.setGroup_id(group_id);
return serviceRepository.save(service);
}
@ -53,13 +46,13 @@ public class ServicesService implements IService<Service, Long> {
@Override
public @Nullable Service update(Service other) {
Service service = serviceRepository.findById(other.getId()).orElse(null);
Service service = serviceRepository.findById(other.getServices_id()).orElse(null);
if (service == null)
return null;
service.setName(other.getName());
service.setCost(other.getCost());
service.setGroup(other.getGroup());
service.setGroup_id(other.getGroup_id());
return serviceRepository.save(service);
}

View File

@ -37,19 +37,10 @@ public class VisitService implements IService<Visit, Long> {
@NotNull Date date,
@NotNull Time time,
@NotNull Boolean isEnd) {
Client client = clientService.get(client_id);
if (client == null)
return null;
Service service = servicesService.get(service_id);
if (service == null)
return null;
Worker worker = workerService.get(worker_id);
if (worker == null)
return null;
Visit visit = new Visit();
visit.setClient(client);
visit.setService(service);
visit.setWorker(worker);
visit.setClient_id(client_id);
visit.setService_id(service_id);
visit.setWorker_id(worker_id);
visit.setDateVisit(date);
visit.setTime(time);
visit.setEnded(isEnd);
@ -79,7 +70,7 @@ public class VisitService implements IService<Visit, Long> {
@Override
public @Nullable Visit update(Visit other) {
Visit visit = visitRepository.findById(other.getId()).orElse(null);
Visit visit = visitRepository.findById(other.getVisit_id()).orElse(null);
if (visit == null)
return null;

View File

@ -27,21 +27,19 @@ public class WorkerService implements IService<Worker, Long> {
@NotNull String telephone,
@NotNull String address,
@NotNull Long post_id) {
Post post = postService.get(post_id);
if (post == null) return null;
Worker out = new Worker();
out.setName(name);
out.setSecondName(secondName);
out.setLastName(lastName);
out.setTelephone(telephone);
out.setAddress(address);
out.setPost(post);
out.setPost_id(post_id);
return workerRepository.save(out);
}
@Override
public @NotNull List<Worker> all() {
return StreamSupport.stream(workerRepository.findAll().spliterator(), false).toList();
return workerRepository.findAll().stream().toList();
}
@Override

View File

@ -16,122 +16,6 @@ public class mappingUtils {
public static <T> List<T> iterableToList(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false).toList();
}
public static ClientDto clientDto(Client client) {
ClientDto clientDto = new ClientDto();
clientDto.setName(client.getName());
clientDto.setId(client.getId());
clientDto.setAddress(client.getAddress());
clientDto.setSecondName(client.getSecondName());
clientDto.setLastName(client.getLastName());
clientDto.setTelephone(client.getTelephone());
clientDto.setVisits(client.getVisits().stream().map(Visit::getId).toList());
return clientDto;
}
public static IdNameDto idNameDto(Long id, String name) {
IdNameDto idNameDto = new IdNameDto();
idNameDto.setId(id);
idNameDto.setName(name);
return idNameDto;
}
public static PostDto postDto(Post post) {
PostDto postDto = new PostDto();
postDto.setId(post.getId());
postDto.setName(post.getName());
postDto.setGroup(
idNameDto(post.getGroup().getId(), post.getGroup().getName())
);
return postDto;
}
public static ServiceDto serviceDto(Service service) {
ServiceDto serviceDto = new ServiceDto();
serviceDto.setCost(service.getCost());
serviceDto.setId(service.getId());
serviceDto.setName(service.getName());
serviceDto.setGroup(
idNameDto(service.getGroup().getId(), service.getGroup().getName())
);
return serviceDto;
}
public static ServiceGroupDto serviceGroupDto(ServiceGroup serviceGroup) {
ServiceGroupDto serviceGroupDto = new ServiceGroupDto();
serviceGroupDto.setId(serviceGroup.getId());
serviceGroupDto.setName(serviceGroup.getName());
return serviceGroupDto;
}
public static VisitDto visitDto(Visit visit) {
VisitDto visitDto = new VisitDto();
visitDto.setId(visit.getId());
visitDto.setDate(visit.getDateVisit());
visitDto.setTime(visit.getTime());
visitDto.setService(serviceDto(visit.getService()));
visitDto.setEnded(visit.isEnded());
visitDto.setClient(
idNameDto(visit.getClient().getId(), visit.getClient().getFio())
);
visitDto.setWorker(
idNameDto(visit.getWorker().getId(), visit.getWorker().getFio())
);
return visitDto;
}
public static WorkerDto workerDto(Worker worker) {
WorkerDto workerDto = new WorkerDto();
workerDto.setName(worker.getName());
workerDto.setPost(postDto(worker.getPost()));
workerDto.setId(worker.getId());
workerDto.setAddress(worker.getAddress());
workerDto.setSecondName(worker.getSecondName());
workerDto.setLastName(worker.getLastName());
workerDto.setTelephone(worker.getTelephone());
workerDto.setVisits(worker.getVisits().stream().map(Visit::getId).toList());
return workerDto;
}
public static Client client(ClientDto clientDto) {
Client client = new Client();
client.setName(clientDto.getName());
client.setId(clientDto.getId());
client.setAddress(clientDto.getAddress());
client.setSecondName(clientDto.getSecondName());
client.setLastName(clientDto.getLastName());
client.setTelephone(clientDto.getTelephone());
return client;
}
public static Post post(PostDto postDto) {
Post post = new Post();
post.setId(postDto.getId());
post.setName(postDto.getName());
return post;
}
public static Service service(ServiceDto serviceDto) {
Service service = new Service();
service.setCost(serviceDto.getCost());
service.setId(serviceDto.getId());
service.setName(serviceDto.getName());
return service;
}
public static ServiceGroup serviceGroup(ServiceGroupDto serviceGroupDto) {
ServiceGroup serviceGroup = new ServiceGroup();
serviceGroup.setId(serviceGroupDto.getId());
serviceGroup.setName(serviceGroupDto.getName());
return serviceGroup;
}
public static Visit visit(VisitDto visitDto) {
Visit visit = new Visit();
visit.setId(visitDto.getId());
visit.setDateVisit(visitDto.getDate());
visit.setTime(visitDto.getTime());
visit.setEnded(visit.isEnded());
return visit;
}
public static Worker worker(WorkerDto workerDto) {
Worker worker = new Worker();
worker.setName(workerDto.getName());
worker.setId(workerDto.getId());
worker.setAddress(workerDto.getAddress());
worker.setSecondName(workerDto.getSecondName());
worker.setLastName(workerDto.getLastName());
worker.setTelephone(workerDto.getTelephone());
return worker;
}
public static Date strToDate(@NotNull String date) {
return Date.valueOf(LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}

View File

@ -8,11 +8,14 @@
spring.thymeleaf.cache=false
spring.datasource.url=jdbc:postgresql://localhost:6666/beautySaloon
spring.datasource.username=postgres
spring.datasource.password=w12344321
#spring.datasource.url=jdbc:postgresql://localhost:6666/beautySaloon
#spring.datasource.username=postgres
#spring.datasource.password=w12344321
#
#spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
#spring.datasource.driver-class-name=org.postgresql.Driver
#spring.jpa.hibernate.ddl-auto=update
#spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.datasource.url="mongodb://localhost:27017/lab7"
spring.jpa.hibernate.ddl-auto=validate

View File

@ -20,7 +20,7 @@
<main class="mt-5 p-2">
<form action="#" th:action="@{/create}" method="post">
<select class="form-select form-select-sm w-100" aria-label="Сотдрудник" th:name="worker">
<option th:each="worker : ${workers}" th:value="${worker.getId()}" th:text="${worker.getName()}"></option>
<option th:each="worker : ${workers}" th:value="${worker.getId()()}" th:text="${worker.getName()}"></option>
</select>
<select class="form-select form-select-sm w-100" aria-label="Клиент" th:name="client">
<option th:each="client : ${clients}" th:value="${client.getId()}" th:text="${client.getName()}"></option>

View File

@ -26,12 +26,12 @@
>
<div class="card-header">
<h5>
Посещение #<span th:text="${visit.getId().toString()}"></span>
Посещение #<span th:text="${visit.getVisit_id().toString()}"></span>
</h5>
</div>
<div class="card-body">
<h6 class="card-text">
<b>Клиент:</b> <span th:text="${visit.getClient().getFio()}"></span>
<b>Клиент:</b> <span th:text="${visit.getClient().getName()}"></span>
</h6>
<h6 class="card-text">
<b>Процедура:</b> <span th:text="${visit.getService().getName()}"></span>
@ -42,7 +42,7 @@
</p>
</div>
<div class="card-footer">
<h6>Цена: <span th:text="${visit.getService().getCost()}"></span>руб.</h6>
<h6>Цена: <span th:text="${visit.getCost()}"></span>руб.</h6>
</div>
</div>
</li>