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

Task is done. #2

Open
wants to merge 1 commit 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
@@ -1,5 +1,8 @@
package com.workintech.fswebs18challengemaven;

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Color;
import com.workintech.fswebs18challengemaven.entity.Type;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand All @@ -8,6 +11,13 @@ public class FswebS18ChallengeMavenApplication {

public static void main(String[] args) {
SpringApplication.run(FswebS18ChallengeMavenApplication.class, args);
Card card = new Card();
card.setId(1L);
card.setColor(Color.HEARTH);
card.setType(Type.ACE);
card.setValue(null);

System.out.println(card.getType());
}

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

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Color;
import com.workintech.fswebs18challengemaven.entity.Type;
import com.workintech.fswebs18challengemaven.exceptions.CardException;
import com.workintech.fswebs18challengemaven.repository.CardRepository;
import com.workintech.fswebs18challengemaven.util.CardValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/cards")
public class CardController {
private CardRepository cardRepository;

@Autowired
public CardController(CardRepository cardRepository) {
this.cardRepository = cardRepository;
}

@GetMapping
public List<Card> getAllCards(){
return cardRepository.findAll();
}

@PostMapping
public Card save(@RequestBody Card card){
return cardRepository.save(card);
}

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

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

@GetMapping("/byValue/{value}")
public List<Card> getByValue(@PathVariable Integer value){
return cardRepository.findByValue(value);
}

@PutMapping("/")
public Card update(@RequestBody Card card){
if (!CardValidation.cardIsValid(card)){
throw new CardException("Card is not valid", HttpStatus.BAD_REQUEST);
}
return cardRepository.update(card);
}

@DeleteMapping("/{id}")
public Card remove(@PathVariable long id){
Card card = cardRepository.remove(id);
if (card == null){
throw new CardException("No Card with id",HttpStatus.NOT_FOUND);
}
return card;
}


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

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Card {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer value;
private Type type;
@Enumerated(EnumType.STRING)
private Color color;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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,7 @@
package com.workintech.fswebs18challengemaven.entity;


public enum Type {
JACK, QUEEN, KING,ACE, JOKER;

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

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

@Data
public class CardErrorResponse {
private String massage;
private HttpStatus status;

public CardErrorResponse(String massage, HttpStatus status) {
this.massage = massage;
this.status = status;
}

public CardErrorResponse(String massage) {
this.massage = massage;
}

public String getMessage() {
return massage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.workintech.fswebs18challengemaven.exceptions;

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

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

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

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
public class GlobalExceptionHandler {

@ExceptionHandler(CardException.class)
public ResponseEntity<CardErrorResponse> cardErrorMassage(CardException cardException){
CardErrorResponse exception = new CardErrorResponse(cardException.getMessage(),cardException.getHttpStatus());
return new ResponseEntity<>(exception,cardException.getHttpStatus());
}

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

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Color;
import com.workintech.fswebs18challengemaven.entity.Type;

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);
Card findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.workintech.fswebs18challengemaven.repository;

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Color;
import com.workintech.fswebs18challengemaven.entity.Type;
import com.workintech.fswebs18challengemaven.exceptions.CardException;
import com.workintech.fswebs18challengemaven.util.CardValidation;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Repository;

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> result = entityManager.createQuery("select c from Card c where c.color = :color",Card.class);
result.setParameter("color",color);
return result.getResultList();
}

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

@Override
public List<Card> findByValue(Integer value) {
TypedQuery<Card> result = entityManager.createQuery("select c from Card c where c.value = :value",Card.class);
result.setParameter("value",value);
return result.getResultList();
}

@Override
public List<Card> findByType(String type) {
TypedQuery<Card> result = entityManager.createQuery("select c from Card c where c.type = :type",Card.class);
result.setParameter("type",type);
return result.getResultList();
}

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

@Transactional
@Override
public Card remove(Long id) {
Card card = entityManager.find(Card.class,id);
if(card != null){
entityManager.remove(card);
}
return card;
}

@Override
public Card findById(Long id) {
return entityManager.find(Card.class,id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.workintech.fswebs18challengemaven.util;

import com.workintech.fswebs18challengemaven.entity.Card;
import com.workintech.fswebs18challengemaven.entity.Type;

public class CardValidation {
public static boolean isIdValid(long id){
return id >= 0;
}

public static boolean isValue(Integer value){
return value >= 0 && value <= 10;
}

public static boolean cardIsValid(Card card) {
if (card == null) {
return false;
}

boolean hasValue = card.getValue() != null;
boolean hasType = card.getType() != null;

if (Type.JOKER.equals(card.getType())) {
return !hasValue && card.getColor() == null;
}

return (hasValue && !hasType) || (!hasValue && hasType);
}




}
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
server.port=9000
server.servlet.context-path=/workintech

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


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;

double score = (double) success / (success + failure);
String userId = "999999";
String userId = "193035";

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;

double score = (double) success / (success + failure);
String userId = "999999";
String userId = "193035";

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