Skip to content

Commit

Permalink
ttt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Epatko committed Mar 24, 2024
1 parent c4be3fd commit 118c2c4
Show file tree
Hide file tree
Showing 47 changed files with 1,460 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.idea/
src/
6 changes: 5 additions & 1 deletion generate-badge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ fi
mkdir -p "$OutDir"

Count=$(hoc -d "$Dir" ${Exclude:+${Exclude[@]}} -s "$Since" -b "$Before" -f "int")

export LC_NUMERIC="en_US"
Count=$(printf "%'d" "$Count")

echo "Hits of code: $Count"

anybadge -l "Hits of Code" -v "$Count" -f "$OutDir/$Filename" -c royalblue
#anybadge -l "Hits of Code" -v "$Count" -f "$OutDir/$Filename" -c royalblue
13 changes: 13 additions & 0 deletions src/main/java/com/example/demo/Application.java
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 src/main/java/com/example/demo/api/v1/cows/controller/CowsApi.java
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);
}
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);
}
}
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();
}
}
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
}
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) {
}
}
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
);
}
}
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;
}
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
) {
}
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) {
}
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);
}
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());
}
}

0 comments on commit 118c2c4

Please sign in to comment.