feat: add api for update photos in batch (#1679)

Signed-off-by: Ryan Wang <i@ryanc.cc>
pull/1682/head
Ryan Wang 2022-02-25 16:09:30 +08:00 committed by GitHub
parent 89c0cb5fb9
commit a94dd8df7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
@ -112,6 +113,22 @@ public class PhotoController {
return new PhotoDTO().convertFrom(photoService.update(photo));
}
@PutMapping("/batch")
@ApiOperation("Updates photo in batch")
public List<PhotoDTO> updateBatchBy(@RequestBody List<@Valid PhotoParam> photoParams) {
List<Photo> photosToUpdate = photoParams.stream()
.filter(photoParam -> Objects.nonNull(photoParam.getId()))
.map(photoParam -> {
Photo photoToUpdate = photoService.getById(photoParam.getId());
photoParam.update(photoToUpdate);
return photoToUpdate;
})
.collect(Collectors.toList());
return photoService.updateInBatch(photosToUpdate).stream()
.map(photo -> (PhotoDTO) new PhotoDTO().convertFrom(photo))
.collect(Collectors.toList());
}
@PutMapping("{photoId:\\d+}/likes")
@ApiOperation("Likes a photo")
@CacheLock(autoDelete = false, traceRequest = true)

View File

@ -15,6 +15,8 @@ import run.halo.app.model.entity.Photo;
@Data
public class PhotoParam implements InputConverter<Photo> {
private Integer id;
@NotBlank(message = "照片名称不能为空")
private String name;