Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Explore with me

Repository for explore with me app - social network for sharing interesting events with users and find
companies to participate in them. Written in java.

# Contributors

[LightInTheFire](https://github.com/LightInTheFire)<br>
[Basarus](https://github.com/Basarus)<br>
[Kreidl](https://github.com/Kreidl)<br>

# Main service db schema

<p align="center">
<img src="assets/readme/main-service-schema.svg" alt="db schema">
</p>

# Stats service db schema

<p align="center">
<img src="assets/readme/stats-service-schema.svg" alt="db schema">
</p>
20 changes: 20 additions & 0 deletions assets/readme/main-service-schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions assets/readme/stats-service-schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions main-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,55 @@
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<classifier>jakarta</classifier>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<classifier>jakarta</classifier>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>dev</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.practicum.category.controller;

import jakarta.validation.Valid;

import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.dto.NewCategoryDto;
import ru.practicum.category.service.CategoryService;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/categories")
public class CategoryAdminController {
private final CategoryService categoryService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CategoryDto createCategory(@Valid @RequestBody NewCategoryDto newCategoryDto) {
log.info("Create category requested {}", newCategoryDto);
return categoryService.createCategory(newCategoryDto);
}

@PatchMapping("/{catId}")
public CategoryDto updateCategory(
@PathVariable Long catId, @Valid @RequestBody NewCategoryDto updateCategoryDto) {
log.info(
"Update category with id={} requested, updated category {}",
catId,
updateCategoryDto);
return categoryService.updateCategory(catId, updateCategoryDto);
}

@DeleteMapping("/{catId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCategory(@PathVariable Long catId) {
log.info("Delete category with id={} requested", catId);
categoryService.deleteCategoryById(catId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.practicum.category.controller;

import java.util.Collection;

import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;

import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.service.CategoryService;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/categories")
public class CategoryPublicController {
private final CategoryService categoryService;

@GetMapping
public Collection<CategoryDto> getAllCategoriesPaged(
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
@RequestParam(defaultValue = "10") @Positive int size) {
log.info("View categories page by page requested from={}, size={}", from, size);
return categoryService.getAllCategoriesPaged(from, size);
}

@GetMapping("/{id}")
public CategoryDto getCategoryById(@PathVariable Long id) {
log.info("View category with id={} requested", id);
return categoryService.getCategoryById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ru.practicum.category.dto;

public record CategoryDto(Long id, String name) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.practicum.category.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record NewCategoryDto(@Size(min = 1, max = 50) @NotBlank String name) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.practicum.category.mapper;

import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.dto.NewCategoryDto;
import ru.practicum.category.model.Category;

import lombok.experimental.UtilityClass;

@UtilityClass
public class CategoryMapper {
public Category mapToEntity(NewCategoryDto newCategoryDto) {
return new Category(null, newCategoryDto.name());
}

public CategoryDto mapToDto(Category category) {
return new CategoryDto(category.getId(), category.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.practicum.category.model;

import jakarta.persistence.*;

import lombok.experimental.FieldDefaults;
import lombok.*;

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "category")
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Column(nullable = false, unique = true)
String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.category.repository;

import ru.practicum.category.model.Category;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryRepository extends JpaRepository<Category, Long> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.practicum.category.service;

import java.util.List;

import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.dto.NewCategoryDto;

public interface CategoryService {
CategoryDto createCategory(NewCategoryDto newCategoryDto);

List<CategoryDto> getAllCategoriesPaged(int from, int size);

CategoryDto getCategoryById(Long id);

void deleteCategoryById(Long id);

CategoryDto updateCategory(Long catId, NewCategoryDto updateCategoryDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.practicum.category.service;

import java.util.List;

import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.dto.NewCategoryDto;
import ru.practicum.category.mapper.CategoryMapper;
import ru.practicum.category.model.Category;
import ru.practicum.category.repository.CategoryRepository;
import ru.practicum.exception.NotFoundException;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;

@Override
public CategoryDto createCategory(NewCategoryDto newCategoryDto) {
Category category = CategoryMapper.mapToEntity(newCategoryDto);
return CategoryMapper.mapToDto(categoryRepository.save(category));
}

@Override
public CategoryDto updateCategory(Long catId, NewCategoryDto updateCategoryDto) {
Category category =
categoryRepository
.findById(catId)
.orElseThrow(
NotFoundException.supplier(
"Category with id=%d was not found", catId));
category.setName(updateCategoryDto.name());
return CategoryMapper.mapToDto(categoryRepository.save(category));
}

@Override
@Transactional(readOnly = true)
public List<CategoryDto> getAllCategoriesPaged(int from, int size) {
Pageable pageable = PageRequest.of(from, size);
return categoryRepository.findAll(pageable).stream().map(CategoryMapper::mapToDto).toList();
}

@Override
@Transactional(readOnly = true)
public CategoryDto getCategoryById(Long id) {
Category category =
categoryRepository
.findById(id)
.orElseThrow(
NotFoundException.supplier(
"Category with id=%d was not found", id));
return CategoryMapper.mapToDto(category);
}

@Override
public void deleteCategoryById(Long id) {
categoryRepository
.findById(id)
.orElseThrow(NotFoundException.supplier("Category with id=%d was not found", id));
categoryRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.practicum.compilation.controller;

import jakarta.validation.Valid;

import ru.practicum.compilation.dto.CompilationDto;
import ru.practicum.compilation.dto.NewCompilationDto;
import ru.practicum.compilation.dto.UpdateCompilationRequest;
import ru.practicum.compilation.service.CompilationsService;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/compilations")
public class CompilationsAdminController {
private final CompilationsService compService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CompilationDto saveCompilation(@RequestBody @Valid NewCompilationDto newCompilationDto) {
log.info("Admin save compilation requested with body={}", newCompilationDto);
return compService.save(newCompilationDto);
}

@DeleteMapping("/{compId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCompilation(@PathVariable long compId) {
log.info("Admin delete compilation requested with id={}", compId);
compService.deleteById(compId);
}

@PatchMapping("/{compId}")
public CompilationDto updateCompilation(
@PathVariable long compId, @RequestBody @Valid UpdateCompilationRequest updateRequest) {
log.info("Admin update compilation requested with id={}, body={}", compId, updateRequest);
return compService.update(compId, updateRequest);
}
}
Loading