import com.greenfoxacademy.orinetationnormalexam.models.APIResult;
import com.greenfoxacademy.orinetationnormalexam.models.Item;
import com.greenfoxacademy.orinetationnormalexam.services.ItemService;
import java.util.List;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class APIController {
private ItemService itemService;
@Autowired
public APIController(ItemService itemService) {
this.itemService = itemService;
}
@GetMapping("/shoppingplanner/query")
public ResponseEntity
<?> selectItems
(@RequestParam
String price, @RequestParam
String type
) {
APIResult apiResult = new APIResult();
List<Item> selectedItems = itemService.selectItems(priceNumber, type);
if (selectedItems.size() > 0) {
apiResult.setResult("ok");
apiResult.setClothes(selectedItems);
HttpStatus status;
status = HttpStatus.OK;
return ResponseEntity.status(status).body(apiResult);
} else {
apiResult.setResult("no clothes available");
apiResult.setClothes(selectedItems);
HttpStatus status;
status = HttpStatus.OK;
return ResponseEntity.status(status).body(apiResult);
}
}
}
// HttpStatus status;
// try {
// linkService.deleteLinkBySecret(secret.getSecretCode(), id);
// status= HttpStatus.NO_CONTENT;
// } catch (IllegalArgumentException e) {
// status=(HttpStatus.FORBIDDEN);
// } catch (NotFoundException e) {
// status=(HttpStatus.NOT_FOUND);
// }
// return ResponseEntity.status(status).build();
@Override
public List
<Item
> selectItems
(Float price,
String type
) {
List<Item> selected = null;
if (type.equalsIgnoreCase("lower")){
selected = itemRepository.findAllByUnitPriceIsLessThan(price);
} else if (type.equalsIgnoreCase("higher")){
selected = itemRepository.findAllByUnitPriceIsGreaterThan(price);
} else if (type.equalsIgnoreCase("equal")){
selected = itemRepository.findAllByUnitPriceIs(price);
}
return selected;
}