-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mikhail Epatko
committed
Mar 24, 2024
1 parent
c4be3fd
commit 118c2c4
Showing
47 changed files
with
1,460 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
.idea/ | ||
src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/demo/api/v1/cows/controller/CowsApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.demo.api.v1.cows.controller; | ||
|
||
import com.example.demo.api.v1.cows.model.request.AddCowRequest; | ||
import com.example.demo.api.v1.cows.model.request.UpdateCowDetailsRequest; | ||
import com.example.demo.api.v1.cows.model.response.FullCowResponse; | ||
import com.example.demo.api.v1.cows.model.response.ShortCowResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
|
||
@Tag(name = "Cows", description = "API для работы с коровами") | ||
public interface CowsApi { | ||
|
||
@Operation(summary = "Запросить всех коров") | ||
List<ShortCowResponse> getAllCows(); | ||
|
||
@Operation(summary = "Запросить всех коров конкретного фермера") | ||
List<ShortCowResponse> farmerCows(long farmerId); | ||
|
||
@Operation(summary = "Запросить корову по ее ID") | ||
FullCowResponse cow(long id); | ||
|
||
@Operation(summary = "Добавить корову фермеру") | ||
void addCowByFarmer(AddCowRequest request); | ||
|
||
@Operation(summary = "Обновить описание коровы") | ||
void updateCowDetails(UpdateCowDetailsRequest request); | ||
|
||
@Operation(summary = "Удалить корову") | ||
void deleteCow(long id); | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/example/demo/api/v1/cows/controller/CowsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.example.demo.api.v1.cows.controller; | ||
|
||
import com.example.demo.api.v1.cows.model.entity.CowEntity; | ||
import com.example.demo.api.v1.cows.model.request.AddCowRequest; | ||
import com.example.demo.api.v1.cows.model.request.UpdateCowDetailsRequest; | ||
import com.example.demo.api.v1.cows.model.response.FullCowResponse; | ||
import com.example.demo.api.v1.cows.model.response.ShortCowResponse; | ||
import com.example.demo.api.v1.cows.service.AddCowToFarmer; | ||
import com.example.demo.api.v1.cows.service.DeleteCowById; | ||
import com.example.demo.api.v1.cows.service.GetAllCows; | ||
import com.example.demo.api.v1.cows.service.GetAllCowsFarmerByFarmerId; | ||
import com.example.demo.api.v1.cows.service.GetCowById; | ||
import com.example.demo.api.v1.cows.service.UpdateCow; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/v1/farmers") | ||
@RequiredArgsConstructor | ||
public class CowsController implements CowsApi { | ||
|
||
private final GetAllCows allCows; | ||
private final GetAllCowsFarmerByFarmerId cowsFarmer; | ||
private final GetCowById cowById; | ||
private final AddCowToFarmer addCowToFarmer; | ||
private final UpdateCow updateCow; | ||
private final DeleteCowById deleteCowById; | ||
|
||
@GetMapping("/cows") | ||
@Override | ||
public List<ShortCowResponse> getAllCows() { | ||
return allCows.execute() | ||
.stream() | ||
.map(CowEntity::toShortResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("/{farmerId}/cows") | ||
@Override | ||
public List<ShortCowResponse> farmerCows(@PathVariable long farmerId) { | ||
return cowsFarmer.execute(farmerId) | ||
.stream() | ||
.map(CowEntity::toShortResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("/cows/{id}") | ||
@Override | ||
public FullCowResponse cow(@PathVariable long id) { | ||
return cowById.execute(id).toFullResponse(); | ||
} | ||
|
||
@PostMapping("/cows") | ||
@Override | ||
public void addCowByFarmer(@RequestBody AddCowRequest request) { | ||
addCowToFarmer.execute(request); | ||
} | ||
|
||
@PutMapping("/cows") | ||
@Override | ||
public void updateCowDetails(@RequestBody UpdateCowDetailsRequest request) { | ||
updateCow.execute(request); | ||
} | ||
|
||
@DeleteMapping("/cows/{id}") | ||
@Override | ||
public void deleteCow(@PathVariable long id) { | ||
deleteCowById.execute(id); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/example/demo/api/v1/cows/model/entity/CowEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.example.demo.api.v1.cows.model.entity; | ||
|
||
import com.example.demo.api.v1.cows.model.enums.Color; | ||
import com.example.demo.api.v1.cows.model.enums.CowStatus; | ||
import com.example.demo.api.v1.cows.model.request.UpdateCowDetailsRequest; | ||
import com.example.demo.api.v1.cows.model.response.FullCowResponse; | ||
import com.example.demo.api.v1.cows.model.response.ShortCowResponse; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "farmer_cow") | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CowEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "farmer_id") | ||
private Long farmerId; | ||
|
||
@Column(name = "name") | ||
private String name; | ||
|
||
@Column(name = "color") | ||
private Color color; | ||
|
||
@Column(name = "liters") | ||
private int liters; | ||
|
||
@Column(name = "status") | ||
private CowStatus status; | ||
|
||
public ShortCowResponse toShortResponse() { | ||
return new ShortCowResponse(id, name, status); | ||
} | ||
|
||
public FullCowResponse toFullResponse() { | ||
return new FullCowResponse(id, farmerId, name, color, liters, status); | ||
} | ||
|
||
public void update(UpdateCowDetailsRequest request) { | ||
if (request.getName() != null) | ||
name = request.getName(); | ||
if (request.getColor() != null) | ||
color = request.getColor(); | ||
if (request.getLiters() != null) | ||
liters = request.getLiters(); | ||
if (request.getStatus() != null) | ||
status = request.getStatus(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/example/demo/api/v1/cows/model/enums/Color.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.demo.api.v1.cows.model.enums; | ||
|
||
public enum Color { | ||
RED, BLACK, BROWN, WHITE | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/demo/api/v1/cows/model/enums/CowStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.demo.api.v1.cows.model.enums; | ||
|
||
public enum CowStatus { | ||
OK("Здоровая, доится"), | ||
REST("В запуске"), | ||
TREATMENT("На лечении"), | ||
DEAD("Умерла"), | ||
SOLD("Продана"); | ||
|
||
CowStatus(String description) { | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/example/demo/api/v1/cows/model/request/AddCowRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.demo.api.v1.cows.model.request; | ||
|
||
import com.example.demo.api.v1.cows.model.entity.CowEntity; | ||
import com.example.demo.api.v1.cows.model.enums.Color; | ||
import com.example.demo.api.v1.cows.model.enums.CowStatus; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AddCowRequest { | ||
|
||
@NotNull(message = "Не может быть null") | ||
@Min(value = 1L, message = "Не может быть меньше 1") | ||
private Long farmerId; | ||
|
||
@NotBlank(message = "Не может быть null или состоять из пробельных символов") | ||
private String name; | ||
|
||
@NotNull(message = "Не может быть null") | ||
private Color color; | ||
|
||
@Min(value = 0, message = "Не может быть меньше 0") | ||
private int liters; | ||
|
||
@NotNull(message = "Не может быть null") | ||
private CowStatus status; | ||
|
||
public CowEntity toEntity() { | ||
return new CowEntity( | ||
null, | ||
farmerId, | ||
name, | ||
color, | ||
liters, | ||
status | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/demo/api/v1/cows/model/request/UpdateCowDetailsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.demo.api.v1.cows.model.request; | ||
|
||
import com.example.demo.api.v1.cows.model.enums.Color; | ||
import com.example.demo.api.v1.cows.model.enums.CowStatus; | ||
import com.example.demo.validation.annotation.NullableNotBlank; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class UpdateCowDetailsRequest { | ||
|
||
@NotNull(message = "Не может быть null") | ||
@Min(value = 1L, message = "Не может быть меньше 1") | ||
private Long id; | ||
|
||
@NullableNotBlank | ||
private String name; | ||
|
||
private Color color; | ||
|
||
@Min(value = 0, message = "Не может быть меньше 0") | ||
private Integer liters; | ||
|
||
private CowStatus status; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/demo/api/v1/cows/model/response/FullCowResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.demo.api.v1.cows.model.response; | ||
|
||
import com.example.demo.api.v1.cows.model.enums.Color; | ||
import com.example.demo.api.v1.cows.model.enums.CowStatus; | ||
|
||
public record FullCowResponse( | ||
Long id, | ||
Long farmerId, | ||
String name, | ||
Color color, | ||
int liters, | ||
CowStatus status | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/demo/api/v1/cows/model/response/ShortCowResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.demo.api.v1.cows.model.response; | ||
|
||
import com.example.demo.api.v1.cows.model.enums.CowStatus; | ||
|
||
public record ShortCowResponse( | ||
Long id, | ||
String name, | ||
CowStatus status) { | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/example/demo/api/v1/cows/repository/CowRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.demo.api.v1.cows.repository; | ||
|
||
import com.example.demo.api.v1.cows.model.entity.CowEntity; | ||
import jakarta.transaction.Transactional; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CowRepository extends JpaRepository<CowEntity, Long> { | ||
|
||
List<CowEntity> findAllByFarmerId(long farmerId); | ||
|
||
@Modifying | ||
@Transactional | ||
@Query(nativeQuery = true, value = "delete from farmer_cow c where c.id = :id") | ||
int deleteCowById(@Param("id") long id); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/example/demo/api/v1/cows/service/AddCowToFarmer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.example.demo.api.v1.cows.service; | ||
|
||
import com.example.demo.api.v1.cows.model.request.AddCowRequest; | ||
import com.example.demo.api.v1.cows.repository.CowRepository; | ||
import com.example.demo.validation.service.ValidateRequest; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class AddCowToFarmer { | ||
|
||
private final CowRepository cowRepository; | ||
private final ValidateRequest validate; | ||
|
||
public void execute(AddCowRequest request) { | ||
log.info("Обработка запроса 'добавить корову фермеру': {}", request); | ||
validate.single(request); | ||
cowRepository.save(request.toEntity()); | ||
} | ||
} |
Oops, something went wrong.