Skip to content
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

It's Done! #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.workintech.fswebs18challengemaven.controller;

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Color;
import com.workintech.fswebs18challengemaven.repository.CardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CardController {

@Autowired
private CardRepository cardRepository;

@GetMapping("/cards")
public List<Card> getCards(){
return cardRepository.findAll();
}

@PostMapping("/cards")
public Card postCard(@RequestBody Card card){
return cardRepository.save(card);
}

@DeleteMapping("/cards/{id}")
public Card deleteCard(@PathVariable long id){
return cardRepository.remove(id);
}

@PutMapping("/cards/")
public Card updateCard(@RequestBody Card card){
return cardRepository.update(card);
}

@GetMapping("/cards/byColor/{color}")
public List<Card> getByColor(@PathVariable String color){
return cardRepository.findByColor(color);
}

@GetMapping("/cards/byType/{type}")
public List<Card> getByType(@PathVariable String type){
return cardRepository.findByType(type);
}

@GetMapping("/cards/byValue/{value}")
public List<Card> getByValue(@PathVariable Integer value){
return cardRepository.findByValue(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.workintech.fswebs18challengemaven.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@NoArgsConstructor
@Data
@Entity
@Table(name = "casino", schema = "public")
public class Card {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "value")
private Integer value;


@Enumerated(EnumType.STRING)
private Type type;

@Enumerated(EnumType.STRING)
private Color color;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.workintech.fswebs18challengemaven.entity;

public enum Color {
SPADE,
HEARTH,
DIAMOND,
CLUB
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.workintech.fswebs18challengemaven.entity;

public class Fntus {
private String name;
private String surname;
private int sayi;

public Fntus(String name, String surname, int sayi) {
this.name = name;
this.surname = surname;
this.sayi = sayi;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public int getSayi() {
return sayi;
}

public void setSayi(int sayi) {
this.sayi = sayi;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.workintech.fswebs18challengemaven.entity;

public enum Type {
JACK,
QUEEN,
KING,
ACE,
JOKER
}
//aa
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.workintech.fswebs18challengemaven.exceptions;

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

@NoArgsConstructor
@AllArgsConstructor
@Data
public class CardErrorResponse {
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.workintech.fswebs18challengemaven.exceptions;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class CardException extends RuntimeException{
private HttpStatus httpStatus;

public CardException(String message, HttpStatus status) {
super(message);
this.httpStatus = status;
}
}
//aa
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.workintech.fswebs18challengemaven.exceptions;


import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

@ExceptionHandler
public ResponseEntity<CardErrorResponse> handleException(CardException cardException){
log.error(cardException.getMessage());
CardErrorResponse errorResponse = new CardErrorResponse(cardException.getMessage());
return new ResponseEntity<>(errorResponse, cardException.getHttpStatus());
}

@ExceptionHandler
public ResponseEntity<CardErrorResponse> handleException(Exception exception){
log.error(exception.getMessage());
CardErrorResponse errorResponse= new CardErrorResponse(exception.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.workintech.fswebs18challengemaven.repository;

import com.workintech.fswebs18challengemaven.entity.Card;

import java.util.List;

public interface CardRepository {

Card save(Card card);
List<Card> findByColor(String color);
List<Card> findAll();
List<Card> findByValue(Integer value);
List<Card> findByType(String type);
Card update(Card card);
Card remove(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.workintech.fswebs18challengemaven.repository;

import com.workintech.fswebs18challengemaven.entity.Card;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
@Repository
public class CardRepositoryImpl implements CardRepository {

private EntityManager entityManager;

@Autowired
public CardRepositoryImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional
@Override
public Card save(Card card) {
entityManager.persist(card);
return card;
}


@Override
public List<Card> findByColor(String color) {
TypedQuery<Card> query = entityManager.createQuery("SELECT c FROM Card c WHERE c.color = :color", Card.class);
return query.getResultList();
}

@Override
public List<Card> findAll() {
TypedQuery<Card> query = entityManager.createQuery("SELECT c FROM Card c", Card.class);
return query.getResultList();
}

@Override
public List<Card> findByValue(Integer value) {
TypedQuery<Card> query = entityManager.createQuery("SELECT c FROM Card WHERE c.value = :value", Card.class);
return query.getResultList();
}

@Override
public List<Card> findByType(String type) {
TypedQuery<Card> query = entityManager.createQuery("SELECT c FROM Card WHERE c.type = :type", Card.class);
return query.getResultList();
}

@Transactional
@Override
public Card update(Card card) {
return entityManager.merge(card);
}

@Transactional
@Override
public Card remove(Long id) {
Card card = entityManager.find(Card.class, id);
entityManager.remove(card);
return card;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ server.port=9000

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=changeme
spring.jpa.hibernate.ddl-auto=update
spring.datasource.password=***
spring.jpa.hibernate.ddl-auto=update;


logging.level.org.hibernate.SQL=Debug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterAll(ExtensionContext context) throws Exception {
long failure = summary.get(TestResultStatus.FAILED) != null ? summary.get(TestResultStatus.FAILED) : 0;

long score = success / (success + failure);
String userId = "999999";
String userId = "189801";

JSONObject json = new JSONObject();
json.put("score", score);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterAll(ExtensionContext context) throws Exception {
long failure = summary.get(TestResultStatus.FAILED) != null ? summary.get(TestResultStatus.FAILED) : 0;

long score = success / (success + failure);
String userId = "999999";
String userId = "189801";

JSONObject json = new JSONObject();
json.put("score", score);
Expand Down