Compare commits

..

No commits in common. "mongodb_lab" and "master" have entirely different histories.

33 changed files with 398 additions and 296 deletions

11
pom.xml
View File

@ -11,7 +11,6 @@
<groupId>com.example</groupId>
<artifactId>sybd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>sybd</name>
<description>sybd</description>
<properties>
@ -22,10 +21,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -57,6 +57,7 @@
<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,13 +1,8 @@
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 {
@ -17,22 +12,5 @@ public class ClientDto {
private String secondName;
private String telephone;
private String address;
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);
}
private List<Long> visits;
}

View File

@ -1,15 +1,9 @@
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,26 +1,10 @@
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 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;
}
private IdNameDto group;
}

View File

@ -1,29 +1,11 @@
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 ServiceGroupDto group;
private IdNameDto 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,17 +1,9 @@
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,13 +1,5 @@
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;
@ -22,36 +14,4 @@ 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,16 +1,8 @@
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 {
@ -21,36 +13,5 @@ public class WorkerDto {
private String telephone;
private String address;
private PostDto post;
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);
}
private List<Long> visits;
}

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

View File

@ -0,0 +1,69 @@
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,24 +1,32 @@
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 org.hibernate.mapping.Set;
import java.text.MessageFormat;
import java.util.Collection;
@Getter
@Setter
@Document(collation = "client")
@Entity
@Table( name = "client" )
public class Client {
@Id
private Long client_id;
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long 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,16 +1,21 @@
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
@Document(collation = "post")
@Entity
@Table( name = "post" )
public class Post {
@Id
private Long post_id;
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private String name;
private Long group_id;
@ManyToOne
@JoinColumn( name = "group_id" )
private ServiceGroup group;
}

View File

@ -1,17 +1,32 @@
package com.example.sybd.models;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Collection;
@Getter
@Setter
@Document( collation = "services" )
@Entity
@Table( name = "services" )
public class Service {
private Long services_id;
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private String name;
private Long group_id;
@ManyToOne
@JoinColumn( name = "group_id" )
private ServiceGroup group;
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,17 +1,22 @@
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
@Document(collation = "services_group")
@Entity
@Table( name = "services_group" )
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
public class ServiceGroup {
@Id
private Long servicesGroup_id;
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
@Column( name = "name" )
private String name;
}

View File

@ -1,22 +1,34 @@
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
@Document(collation = "visit")
@Entity
@Table( name = "visit" )
public class Visit {
@Id
private Long visit_id;
private Long client_id;
private Long service_id;
private Long worker_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 Date dateVisit;
private Time time;
private boolean isEnded;

View File

@ -1,28 +1,42 @@
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
@Document(collation = "worker")
@Entity
@Table( name = "worker" )
public class Worker {
@Id
private Long worker_id;
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "id", nullable = false )
private Long id;
private String name;
private String lastName;
private String secondName;
private String telephone;
private String address;
private Long post_id;
@ManyToOne
@JoinColumn( name = "post_id" )
private Post post;
@OneToMany( mappedBy = "worker" )
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.mongodb.repository.MongoRepository;
import org.springframework.data.repository.CrudRepository;
public interface ClientRepository extends MongoRepository<Client, Long> {
public interface ClientRepository extends CrudRepository<Client, Long> {
}

View File

@ -1,8 +1,7 @@
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 MongoRepository<Post, Long> {
public interface PostRepository extends CrudRepository<Post, Long> {
}

View File

@ -1,10 +1,9 @@
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 MongoRepository<ServiceGroup, Long> {
public interface ServiceGroupRepository extends CrudRepository<ServiceGroup, Long> {
}

View File

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

View File

@ -1,8 +1,7 @@
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 MongoRepository<Visit, Long> {
public interface VisitRepository extends CrudRepository<Visit, Long> {
}

View File

@ -1,12 +1,11 @@
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 MongoRepository<Worker, Long> {
public interface WorkerRepository extends CrudRepository<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.getClient_id()).orElse(null);
Client client = clientRepository.findById(other.getId()).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.getServicesGroup_id()).orElse(null);
ServiceGroup serviceGroup = serviceGroupRepository.findById(other.getId()).orElse(null);
if (serviceGroup == null)
return null;

View File

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

View File

@ -12,16 +12,23 @@ 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) {
public ServicesService(ServiceRepository serviceRepository, GroupServiceService groupServiceService) {
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_id(group_id);
service.setGroup(serviceGroup);
return serviceRepository.save(service);
}
@ -46,13 +53,13 @@ public class ServicesService implements IService<Service, Long> {
@Override
public @Nullable Service update(Service other) {
Service service = serviceRepository.findById(other.getServices_id()).orElse(null);
Service service = serviceRepository.findById(other.getId()).orElse(null);
if (service == null)
return null;
service.setName(other.getName());
service.setCost(other.getCost());
service.setGroup_id(other.getGroup_id());
service.setGroup(other.getGroup());
return serviceRepository.save(service);
}

View File

@ -37,10 +37,19 @@ 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_id(client_id);
visit.setService_id(service_id);
visit.setWorker_id(worker_id);
visit.setClient(client);
visit.setService(service);
visit.setWorker(worker);
visit.setDateVisit(date);
visit.setTime(time);
visit.setEnded(isEnd);
@ -70,7 +79,7 @@ public class VisitService implements IService<Visit, Long> {
@Override
public @Nullable Visit update(Visit other) {
Visit visit = visitRepository.findById(other.getVisit_id()).orElse(null);
Visit visit = visitRepository.findById(other.getId()).orElse(null);
if (visit == null)
return null;

View File

@ -27,19 +27,21 @@ 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_id(post_id);
out.setPost(post);
return workerRepository.save(out);
}
@Override
public @NotNull List<Worker> all() {
return workerRepository.findAll().stream().toList();
return StreamSupport.stream(workerRepository.findAll().spliterator(), false).toList();
}
@Override

View File

@ -16,6 +16,122 @@ 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,14 +8,11 @@
spring.thymeleaf.cache=false
#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.datasource.url=jdbc:postgresql://localhost:6666/beautySaloon
spring.datasource.username=postgres
spring.datasource.password=w12344321
spring.datasource.url="mongodb://localhost:27017/lab7"
spring.jpa.hibernate.ddl-auto=validate
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

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.getVisit_id().toString()}"></span>
Посещение #<span th:text="${visit.getId().toString()}"></span>
</h5>
</div>
<div class="card-body">
<h6 class="card-text">
<b>Клиент:</b> <span th:text="${visit.getClient().getName()}"></span>
<b>Клиент:</b> <span th:text="${visit.getClient().getFio()}"></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.getCost()}"></span>руб.</h6>
<h6>Цена: <span th:text="${visit.getService().getCost()}"></span>руб.</h6>
</div>
</div>
</li>