Skip to content

Commit a6f6d29

Browse files
committed
update
1 parent 393e3bf commit a6f6d29

File tree

11 files changed

+69
-69
lines changed

11 files changed

+69
-69
lines changed

src/main/java/io/hexlet/project_devops_deploy/dto/BulletinDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.hexlet.project_devops_deploy.dto;
22

3-
import io.hexlet.project_devops_deploy.model.BulletinState;
3+
import io.hexlet.project_devops_deploy.model.bulletin.BulletinState;
44
import lombok.AllArgsConstructor;
55
import lombok.Builder;
66
import lombok.Getter;

src/main/java/io/hexlet/project_devops_deploy/dto/BulletinRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.hexlet.project_devops_deploy.dto;
22

3-
import io.hexlet.project_devops_deploy.model.BulletinState;
3+
import io.hexlet.project_devops_deploy.model.bulletin.BulletinState;
44
import jakarta.validation.constraints.NotBlank;
55
import jakarta.validation.constraints.NotNull;
66
import lombok.AllArgsConstructor;
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.hexlet.project_devops_deploy.exception;
22

3+
34
import org.springframework.http.HttpStatus;
45
import org.springframework.web.bind.annotation.ResponseStatus;
56

67
@ResponseStatus(HttpStatus.NOT_FOUND)
7-
public class BulletinNotFoundException extends RuntimeException {
8-
9-
public BulletinNotFoundException(Long id) {
10-
super("Bulletin %d not found".formatted(id));
8+
public class ResourceNotFoundException extends RuntimeException {
9+
public ResourceNotFoundException(String message) {
10+
super(message);
1111
}
1212
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.hexlet.project_devops_deploy.mapper;
2+
3+
import org.mapstruct.Condition;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.MappingConstants;
6+
import org.openapitools.jackson.nullable.JsonNullable;
7+
8+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
9+
public abstract class JsonNullableMapper {
10+
11+
/**
12+
* Checks whether nullable parameter was passed explicitly.
13+
*
14+
* @return true if value was set explicitly, false otherwise
15+
*/
16+
@Condition
17+
public <T> boolean isPresent(JsonNullable<T> nullable) {
18+
return nullable != null && nullable.isPresent();
19+
}
20+
21+
public <T> T unwrap(JsonNullable<T> jsonNullable) {
22+
return jsonNullable == null ? null : jsonNullable.orElse(null);
23+
}
24+
25+
public <T> JsonNullable<T> wrap(T entity) {
26+
return JsonNullable.of(entity);
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.hexlet.project_devops_deploy.mapper;
2+
3+
import io.hexlet.project_devops_deploy.model.BaseEntity;
4+
import jakarta.persistence.EntityManager;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.MappingConstants;
7+
import org.mapstruct.TargetType;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
11+
public abstract class ReferenceMapper {
12+
@Autowired
13+
private EntityManager entityManager;
14+
15+
public <T extends BaseEntity> T toEntity(Long id, @TargetType Class<T> entityClass) {
16+
return id != null ? entityManager.find(entityClass, id) : null;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package io.hexlet.project_devops_deploy.model;
22

3-
public enum BulletinState {
4-
DRAFT, PUBLISHED
3+
public interface BaseEntity {
54
}

src/main/java/io/hexlet/project_devops_deploy/model/Bulletin.java

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
import jakarta.persistence.GenerationType;
1010
import jakarta.persistence.Id;
1111
import jakarta.persistence.Table;
12+
import io.hexlet.project_devops_deploy.model.bulletin.BulletinState;
1213
import java.time.LocalDateTime;
14+
import lombok.Getter;
15+
import lombok.Setter;
1316
import org.springframework.data.annotation.CreatedDate;
1417
import org.springframework.data.annotation.LastModifiedDate;
1518
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
1619

20+
@Getter
21+
@Setter
1722
@Entity
1823
@Table(name = "bulletins")
1924
@EntityListeners(AuditingEntityListener.class)
20-
public class Bulletin {
25+
public class Bulletin implements BaseEntity {
2126

2227
@Id
2328
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -44,59 +49,4 @@ public class Bulletin {
4449
@Column(name = "updated_at", nullable = false)
4550
private LocalDateTime updatedAt;
4651

47-
public String getContact() {
48-
return contact;
49-
}
50-
51-
public String getDescription() {
52-
return description;
53-
}
54-
55-
public Long getId() {
56-
return id;
57-
}
58-
59-
public BulletinState getState() {
60-
return state;
61-
}
62-
63-
public String getTitle() {
64-
return title;
65-
}
66-
67-
public void setContact(String contact) {
68-
this.contact = contact;
69-
}
70-
71-
public void setDescription(String description) {
72-
this.description = description;
73-
}
74-
75-
public void setId(Long id) {
76-
this.id = id;
77-
}
78-
79-
public void setState(BulletinState state) {
80-
this.state = state;
81-
}
82-
83-
public void setTitle(String title) {
84-
this.title = title;
85-
}
86-
87-
public LocalDateTime getCreatedAt() {
88-
return createdAt;
89-
}
90-
91-
public void setCreatedAt(LocalDateTime createdAt) {
92-
this.createdAt = createdAt;
93-
}
94-
95-
public LocalDateTime getUpdatedAt() {
96-
return updatedAt;
97-
}
98-
99-
public void setUpdatedAt(LocalDateTime updatedAt) {
100-
this.updatedAt = updatedAt;
101-
}
10252
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.hexlet.project_devops_deploy.model.bulletin;
2+
3+
public enum BulletinState {
4+
DRAFT, PUBLISHED
5+
}

src/main/java/io/hexlet/project_devops_deploy/service/BulletinService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import io.hexlet.project_devops_deploy.dto.BulletinDto;
44
import io.hexlet.project_devops_deploy.dto.BulletinRequest;
5-
import io.hexlet.project_devops_deploy.exception.BulletinNotFoundException;
6-
import io.hexlet.project_devops_deploy.mapper.BulletinMapper;
5+
import io.hexlet.project_devops_deploy.exception.ResourceNotFoundException;
76
import io.hexlet.project_devops_deploy.model.Bulletin;
7+
import io.hexlet.project_devops_deploy.mapper.BulletinMapper;
88
import io.hexlet.project_devops_deploy.repository.BulletinRepository;
99
import java.util.List;
1010
import org.springframework.stereotype.Service;
@@ -43,7 +43,7 @@ public BulletinDto findById(Long id) {
4343
}
4444

4545
private Bulletin getBulletin(Long id) {
46-
return repository.findById(id).orElseThrow(() -> new BulletinNotFoundException(id));
46+
return repository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Bulletin has not found"));
4747
}
4848

4949
public BulletinDto update(Long id, BulletinRequest request) {

src/main/java/io/hexlet/project_devops_deploy/util/ModelGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.hexlet.project_devops_deploy.util;
22

33
import io.hexlet.project_devops_deploy.model.Bulletin;
4-
import io.hexlet.project_devops_deploy.model.BulletinState;
4+
import io.hexlet.project_devops_deploy.model.bulletin.BulletinState;
55
import jakarta.annotation.PostConstruct;
66
import lombok.Getter;
77
import net.datafaker.Faker;

0 commit comments

Comments
 (0)