-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Пришлось немного подредактировать application.yml из-за того, что pos… #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dsr.amm.homebudget.controller; | ||
|
||
|
||
import dsr.amm.homebudget.data.dto.CategoryDTO; | ||
import dsr.amm.homebudget.service.impl.CategoryService; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Trokhin | ||
* | ||
*/ | ||
|
||
@RestController | ||
@RequestMapping("/categories") | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
public CategoryController(CategoryService categoryService) { | ||
this.categoryService = categoryService; | ||
} | ||
|
||
@GetMapping("/all") | ||
public List<CategoryDTO> getAll() { | ||
return categoryService.getAll(); | ||
} | ||
@GetMapping("/{id}") | ||
public CategoryDTO getByID(@PathVariable("id") Long id) { | ||
CategoryDTO categoryDTO = categoryService.get(id); | ||
|
||
if(categoryDTO == null) | ||
throw new IllegalArgumentException(); | ||
else | ||
return categoryDTO; | ||
} | ||
@PostMapping | ||
public CategoryDTO create(@RequestBody CategoryDTO categoryDTO) { | ||
return categoryService.create(categoryDTO); | ||
} | ||
@DeleteMapping("/{id}") | ||
public CategoryDTO delete(@PathVariable("id") Long id) { | ||
return categoryService.delete(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dsr.amm.homebudget.data.dto; | ||
|
||
|
||
/** | ||
* @author Trokhin | ||
* | ||
* Поле для ownerId пока не мапится, но оно пока и не нужно, его легко убрать если оно станет точно ненужным, а пока пусть будет. | ||
* Так же из-за невозможности его замапить, все создаваемые категории получаются с ownerId = null. | ||
*/ | ||
public class CategoryDTO { | ||
private Long id; | ||
private String name; | ||
private String description; | ||
|
||
private Long ownerId; | ||
|
||
//id | ||
public Long getId() { | ||
return id; | ||
} | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
//name | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
//description | ||
public String getDescription() { | ||
return description; | ||
} | ||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
//ownerId | ||
public Long getOwnerId() { | ||
return ownerId; | ||
} | ||
public void setOwnerId(Long ownerId) { | ||
this.ownerId = ownerId; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dsr.amm.homebudget.data.repository; | ||
|
||
import dsr.amm.homebudget.data.entity.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* @author Trokhin | ||
* | ||
* Здесь я решил использовать {@code JpaRepository}, хотя пока это и не сильно оправдано, но в будущем может пригодиться. | ||
*/ | ||
|
||
@Repository | ||
public interface ICategoryRepository extends JpaRepository<Category, Long> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dsr.amm.homebudget.service; | ||
|
||
import dsr.amm.homebudget.data.dto.CategoryDTO; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Trokhin | ||
* | ||
*/ | ||
public interface ICategoryService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пока что интерфейс выглядит излишним. Хотя, конечно, так тоже можно. |
||
|
||
/** | ||
* | ||
* @param categoryDTO - dto, передаваемое в запрос, которое надо создать и поместить в базу данных | ||
* @return - созданный объект | ||
*/ | ||
public CategoryDTO create(CategoryDTO categoryDTO); | ||
|
||
/** | ||
* | ||
* @param categoryDTO - который надо удалить | ||
* @return - удаленный объект | ||
*/ | ||
public CategoryDTO delete(CategoryDTO categoryDTO); | ||
|
||
/** | ||
* | ||
* @return список объектов хранящихся в базе данных | ||
*/ | ||
public List<CategoryDTO> getAll(); | ||
|
||
/** | ||
* | ||
* @param id - id необходимого нам объекта | ||
* @return полученный объект | ||
*/ | ||
public CategoryDTO get(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dsr.amm.homebudget.service.impl; | ||
|
||
import dsr.amm.homebudget.OrikaMapper; | ||
import dsr.amm.homebudget.data.dto.CategoryDTO; | ||
import dsr.amm.homebudget.data.entity.Category; | ||
import dsr.amm.homebudget.data.repository.ICategoryRepository; | ||
import dsr.amm.homebudget.service.ICategoryService; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* | ||
* @author Trokhin | ||
*/ | ||
|
||
@Service | ||
public class CategoryService implements ICategoryService { | ||
|
||
private final ICategoryRepository categoryRepository; | ||
private final OrikaMapper mapper; | ||
|
||
public CategoryService(ICategoryRepository repository, OrikaMapper mapper) { | ||
this.categoryRepository = repository; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public CategoryDTO create(CategoryDTO categoryDTO) { | ||
Category category = fromDTO(categoryDTO); | ||
|
||
Category savedCategory = categoryRepository.save(category); | ||
return toDTO(savedCategory); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public CategoryDTO delete(CategoryDTO categoryDTO) { | ||
return delete(categoryDTO.getId()); | ||
} | ||
|
||
public CategoryDTO delete(Long id) { | ||
Optional<Category> category = categoryRepository.findById(id); | ||
|
||
category.ifPresent(categoryRepository::delete); | ||
|
||
return toDTO(category.get()); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public List<CategoryDTO> getAll() { | ||
return toDTO((List<Category>) categoryRepository.findAll()); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public CategoryDTO get(Long id) { | ||
Optional<Category> category = categoryRepository.findById(id); | ||
|
||
return category.map(this::toDTO).orElse(null); | ||
} | ||
|
||
private Category fromDTO(CategoryDTO categoryDTO) { | ||
return mapper.map(categoryDTO, Category.class); | ||
} | ||
private CategoryDTO toDTO(Category category) { | ||
return mapper.map(category, CategoryDTO.class); | ||
} | ||
private List<CategoryDTO> toDTO(List<Category> list) { | ||
return mapper.mapAsList(list, CategoryDTO.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ spring: | |
generate-ddl: true # Whether to initialize the schema on startup. | ||
hibernate.ddl-auto: update | ||
datasource: | ||
url: jdbc:postgresql://localhost:5432/home-budget | ||
username: user | ||
password: myPass | ||
url: jdbc:postgresql://localhost:5432/homebudget | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну с такой правкой оно уже у меня не будет работать :-) Это выглядит как правка, которая важна только для вашего окружения. Такие вещи лучше не коммитить, по крайней мере, в глобальный файл |
||
username: myuser | ||
password: qwerty | ||
driver-class-name: org.postgresql.Driver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не проще ли бросить exception сразу из сервиса? Всегда проще работать в предположении, что сервис не вернул null (ибо что с ним делать-то?).