Skip to content

Commit

Permalink
[OT276-42] Endpoint Detalle Categoría (#51)
Browse files Browse the repository at this point in the history
* Added getByIdIfExists() method to CategoryService

* Implementation of getByIdIfExists() method in CategoryServiceImpl

* Creation of CategoryResponse in response package

* Added categoryToCategoryResponse() mapper to CategoryMapperController

* Added getCategory endpoint with path 'v1/categories/{id}' to CategoryController

* Added configuration for path 'v1/categories/{id}' in SecurityConfig
  • Loading branch information
Juacun authored Sep 7, 2022
1 parent a36bbb1 commit 838f3fc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/alkemy/ong/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ protected void configure(HttpSecurity http) throws Exception {
.authorizeHttpRequests()
.antMatchers("/api/docs/**", "/api/swagger-ui/**", "/v3/api-docs/**", "/auth/login", "/auth/register").permitAll()
.antMatchers(HttpMethod.GET, "/v1/organizations/public/{id}").permitAll()
.antMatchers(HttpMethod.POST,ApiConstants.CONTACTS_URI).permitAll()
.antMatchers(HttpMethod.POST, ApiConstants.CONTACTS_URI).permitAll()
.antMatchers(HttpMethod.PUT, ApiConstants.MEMBERS_URI + "/{id}").authenticated()
.antMatchers(HttpMethod.GET, ApiConstants.USERS_URI).hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH, ApiConstants.USERS_URI + "/{id}").hasAnyRole("USER", "ADMIN")
.antMatchers(HttpMethod.DELETE, ApiConstants.USERS_URI + "/{id}").hasAnyRole("USER", "ADMIN")
.antMatchers(HttpMethod.GET, ApiConstants.SLIDES_URI + "/{id}").hasRole("ADMIN")
.antMatchers(HttpMethod.GET, ApiConstants.CATEGORIES_URI + "/{id}").hasRole("ADMIN")
.antMatchers(HttpMethod.GET).authenticated()
.antMatchers(HttpMethod.POST).hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH).hasRole("ADMIN")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public interface CategoryService {

Long createEntity(Category category);

Category getByIdIfExists(Long id);

void deleteById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alkemy.ong.core.usecase.impl;

import com.alkemy.ong.config.exception.NotFoundException;
import com.alkemy.ong.core.model.Category;
import com.alkemy.ong.core.repository.CategoryRepository;
import com.alkemy.ong.core.usecase.CategoryService;
Expand All @@ -19,6 +20,12 @@ public Long createEntity(Category category) {
return categoryRepository.save(category).getId();
}

@Override
@Transactional(readOnly = true)
public Category getByIdIfExists(Long id) {
return categoryRepository.findById(id).orElseThrow(() -> new NotFoundException(id));
}

@Override
@Transactional
public void deleteById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import com.alkemy.ong.ports.input.rs.api.CategoryApi;
import com.alkemy.ong.ports.input.rs.mapper.CategoryMapperController;
import com.alkemy.ong.ports.input.rs.request.CreateCategoryRequest;
import com.alkemy.ong.ports.input.rs.response.CategoryResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
Expand Down Expand Up @@ -49,10 +51,18 @@ public ResponseEntity<Void> createCategory(@Valid @RequestBody CreateCategoryReq
return ResponseEntity.created(location).build();
}

@GetMapping("/{id}")
public ResponseEntity<CategoryResponse> getCategory(@NotNull @PathVariable Long id) {
Category category = categoryService.getByIdIfExists(id);
CategoryResponse response = mapper.categoryToCategoryResponse(category);
return ResponseEntity.ok(response);
}

@Override
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCategoryById(@NotNull @PathVariable("id") Long id) {
categoryService.deleteById(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.alkemy.ong.core.model.Category;
import com.alkemy.ong.ports.input.rs.request.CreateCategoryRequest;
import com.alkemy.ong.ports.input.rs.response.CategoryResponse;
import jdk.jfr.Name;
import org.mapstruct.Mapper;
import org.mapstruct.Named;

@Mapper
public interface CategoryMapperController extends CommonMapper{
public interface CategoryMapperController extends CommonMapper {

@Named("createCategoryRequestToCategory")
Category createCategoryRequestToCategory(CreateCategoryRequest createCategoryRequest);

@Name("CategoryToCategoryResponse")
CategoryResponse categoryToCategoryResponse(Category category);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alkemy.ong.ports.input.rs.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CategoryResponse {

private Long id;

private String name;

private String description;

private String image;

}

0 comments on commit 838f3fc

Please sign in to comment.