feat: add addFirearm endpoint and FirearmRequest DTO for firearm creation
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.onixbyte.deltaforceguide.controller;
|
package com.onixbyte.deltaforceguide.controller;
|
||||||
|
|
||||||
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmRequest;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
||||||
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
||||||
@@ -11,11 +12,7 @@ import jakarta.validation.constraints.Min;
|
|||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@Tag(name = "武器管理", description = "与武器有关的操作")
|
@Tag(name = "武器管理", description = "与武器有关的操作")
|
||||||
@RestController
|
@RestController
|
||||||
@@ -46,5 +43,10 @@ public class FirearmController {
|
|||||||
public FirearmResponse queryById(@PathVariable Long id) {
|
public FirearmResponse queryById(@PathVariable Long id) {
|
||||||
return firearmService.queryById(id);
|
return firearmService.queryById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public FirearmResponse addFirearm(@Validated @RequestBody FirearmRequest request) {
|
||||||
|
return firearmService.addFirearm(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.onixbyte.deltaforceguide.domain.dto;
|
||||||
|
|
||||||
|
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
||||||
|
|
||||||
|
public record FirearmRequest(
|
||||||
|
String name,
|
||||||
|
FirearmType type,
|
||||||
|
String level,
|
||||||
|
String calibre,
|
||||||
|
Integer fireRate,
|
||||||
|
Integer armourDamage,
|
||||||
|
Integer bodyDamage,
|
||||||
|
String review
|
||||||
|
) {
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.onixbyte.deltaforceguide.service;
|
package com.onixbyte.deltaforceguide.service;
|
||||||
|
|
||||||
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmRequest;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
|
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
|
||||||
@@ -36,5 +37,20 @@ public class FirearmService {
|
|||||||
.map(FirearmResponse::from)
|
.map(FirearmResponse::from)
|
||||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Firearm not found: " + id));
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Firearm not found: " + id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FirearmResponse addFirearm(FirearmRequest request) {
|
||||||
|
var firearm = firearmRepository.save(Firearm.builder()
|
||||||
|
.name(request.name())
|
||||||
|
.type(request.type())
|
||||||
|
.level(request.level())
|
||||||
|
.calibre(request.calibre())
|
||||||
|
.fireRate(request.fireRate())
|
||||||
|
.armourDamage(request.armourDamage())
|
||||||
|
.bodyDamage(request.bodyDamage())
|
||||||
|
.review(request.review())
|
||||||
|
.build());
|
||||||
|
|
||||||
|
return FirearmResponse.from(firearm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user