Skip to content
Snippets Groups Projects
Commit 1325de52 authored by Anders Montsko Austlid's avatar Anders Montsko Austlid
Browse files

Merge branch 'epic/achievement-endpoint' into 'main'

Epic/achievement endpoint

See merge request idatt2106-v23-03/backend!52
parents 1681d5f7 97fbdbc8
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.controller.user;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.user.Achievement;
import ntnu.idatt2016.v233.SmartMat.service.user.AchievementService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
/**
* Achievement API endpoint, providing endpoints for fetching an achievement by name,
* and fetching all achievements stored in the database
*
* @author Anders
* @version 1.0
* @sinc 20.04.2023
*/
@AllArgsConstructor
@RestController
@RequestMapping("/api/achievements")
public class AchievementController {
private final AchievementService achievementService;
/**
* Gets an achievement from the database
* @param achievementName name of achievement to get
* @return a ResponseEntity containing the achievement if it exists, or a 404 if it doesn't
*/
@GetMapping("/achievement/{achievementName}")
public ResponseEntity<Achievement> getAchievement(@PathVariable("achievementName") String achievementName){
Optional<Achievement> achievement = achievementService.getAchievement(achievementName);
return achievement.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Gets all achievements from the database
* @return a ResponseEntity containing a list of all achievements, or a 404 if there are none
*/
@GetMapping("/all")
public ResponseEntity<List<Achievement>> getAchievements(){
List<Achievement> achievements = achievementService.getAchievements();
return achievements.isEmpty() ? ResponseEntity.notFound().build() : ResponseEntity.ok(achievements);
}
}
......@@ -5,6 +5,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.user.Achievement;
import ntnu.idatt2016.v233.SmartMat.repository.user.AchievementRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
......@@ -21,18 +22,6 @@ public class AchievementService {
private AchievementRepository achievementRepository;
/**
* Adds an achievement to the database
* @param achievementName name of achievement to add
* @param achievementDescription description of achievement to add
*/
public void addAchievement(String achievementName, String achievementDescription){
achievementRepository.save(Achievement.builder()
.achievementName(achievementName)
.achievementDescription(achievementDescription)
.build());
}
/**
* Gets an achievement from the database
* @param achievementName name of achievement to get
......@@ -41,4 +30,8 @@ public class AchievementService {
public Optional<Achievement> getAchievement(String achievementName){
return achievementRepository.findByAchievementName(achievementName);
}
public List<Achievement> getAchievements(){
return achievementRepository.findAll();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment