fix a few things

This commit is contained in:
Jake Potrebic 2021-03-22 17:40:57 -07:00
parent fb4990e8ce
commit b07d78f059
No known key found for this signature in database
GPG Key ID: 7C58557EC9C421F8
6 changed files with 70 additions and 69 deletions

View File

@ -1,8 +1,14 @@
package io.papermc.hangar.controller.internal;
import io.papermc.hangar.controller.HangarController;
import io.papermc.hangar.model.api.requests.FlagForm;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.internal.projects.HangarProjectFlag;
import io.papermc.hangar.security.annotations.LoggedIn;
import io.papermc.hangar.security.annotations.permission.PermissionRequired;
import io.papermc.hangar.service.internal.admin.FlagService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -14,14 +20,9 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
import io.papermc.hangar.controller.HangarController;
import io.papermc.hangar.controllerold.forms.FlagForm;
import io.papermc.hangar.model.internal.projects.HangarProjectFlag;
import io.papermc.hangar.service.internal.admin.FlagService;
@LoggedIn
@Controller
@Secured("ROLE_USER")
@RequestMapping(path = "/api/internal/flags", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping("/api/internal/flags")
public class FlagController extends HangarController {
private final FlagService flagService;
@ -30,26 +31,29 @@ public class FlagController extends HangarController {
this.flagService = flagService;
}
@PostMapping("/")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PostMapping(value = "/", consumes = MediaType.APPLICATION_JSON_VALUE)
public void flag(@RequestBody FlagForm form) {
flagService.createFlag(form.getProjectId(), form.getReason(), form.getComment());
}
@PostMapping("/{id}/resolve/{resolve}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void resolve(@PathVariable("id") long id, @PathVariable("resolve") boolean resolve) {
@PostMapping("/{id}/resolve/{resolve}")
@PermissionRequired(perms = NamedPermission.MOD_NOTES_AND_FLAGS)
public void resolve(@PathVariable long id, @PathVariable boolean resolve) {
flagService.markAsResolved(id, resolve);
}
@ResponseBody
@GetMapping("/{author}/{slug}")
public List<HangarProjectFlag> getFlags(@PathVariable("author") String author, @PathVariable("slug") String slug) {
@GetMapping(value = "/{author}/{slug}", produces = MediaType.APPLICATION_JSON_VALUE)
@PermissionRequired(perms = NamedPermission.MOD_NOTES_AND_FLAGS)
public List<HangarProjectFlag> getFlags(@PathVariable String author, @PathVariable String slug) {
return flagService.getFlags(author, slug);
}
@ResponseBody
@GetMapping("/")
@GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
@PermissionRequired(perms = NamedPermission.MOD_NOTES_AND_FLAGS)
public List<HangarProjectFlag> getFlags() {
return flagService.getFlags();
}

View File

@ -1,46 +0,0 @@
package io.papermc.hangar.controllerold.forms;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.jetbrains.annotations.NotNull;
import java.util.StringJoiner;
import io.papermc.hangar.model.common.projects.FlagReason;
public class FlagForm {
private final String comment;
private final long projectId;
private final FlagReason reason;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public FlagForm(@JsonProperty(value = "comment", required = true) @NotNull String comment,
@JsonProperty(value = "project_id", required = true) long projectId,
@JsonProperty(value = "reason", required = true) @NotNull FlagReason reason) {
this.comment = comment;
this.projectId = projectId;
this.reason = reason;
}
public String getComment() {
return comment;
}
public long getProjectId() {
return projectId;
}
public FlagReason getReason() {
return reason;
}
@Override
public String toString() {
return new StringJoiner(", ", FlagForm.class.getSimpleName() + "[", "]")
.add("comment='" + comment + "'")
.add("projectId=" + projectId)
.add("reason=" + reason)
.toString();
}
}

View File

@ -0,0 +1,43 @@
package io.papermc.hangar.model.api.requests;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.papermc.hangar.model.common.projects.FlagReason;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
public class FlagForm {
@NotBlank
private final String comment;
private final long projectId;
@NotNull
private final FlagReason reason;
@JsonCreator
public FlagForm(String comment, long projectId, FlagReason reason) {
this.comment = comment;
this.projectId = projectId;
this.reason = reason;
}
public String getComment() {
return comment;
}
public long getProjectId() {
return projectId;
}
public FlagReason getReason() {
return reason;
}
@Override
public String toString() {
return "FlagForm{" +
"comment='" + comment + '\'' +
", projectId=" + projectId +
", reason=" + reason +
'}';
}
}

View File

@ -83,7 +83,7 @@ public class ProjectFlagTable extends Table {
"projectId=" + projectId +
", userId=" + userId +
", reason=" + reason +
", isResolved=" + resolved +
", resolved=" + resolved +
", comment='" + comment + '\'' +
", resolvedAt=" + resolvedAt +
", resolvedBy=" + resolvedBy +

View File

@ -1,16 +1,15 @@
package io.papermc.hangar.service.internal.admin;
import org.springframework.stereotype.Service;
import java.time.OffsetDateTime;
import java.util.List;
import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.dao.internal.HangarFlagsDAO;
import io.papermc.hangar.model.common.projects.FlagReason;
import io.papermc.hangar.model.db.projects.ProjectFlagTable;
import io.papermc.hangar.model.internal.projects.HangarProjectFlag;
import io.papermc.hangar.service.HangarService;
import org.springframework.stereotype.Service;
import java.time.OffsetDateTime;
import java.util.List;
@Service
public class FlagService extends HangarService {
@ -23,12 +22,13 @@ public class FlagService extends HangarService {
public void createFlag(long projectId, FlagReason reason, String comment) {
// TODO idk, we prolly need more checking here, plus notification? logs?
flagsDAO.insert(new ProjectFlagTable( projectId, getHangarUserId(), reason, comment));
flagsDAO.insert(new ProjectFlagTable( projectId, getHangarPrincipal().getId(), reason, comment));
}
public ProjectFlagTable markAsResolved(long flagId, boolean resolved) {
Long resolvedBy = resolved ? getHangarPrincipal().getId() : null;
OffsetDateTime resolvedAt = resolved ? OffsetDateTime.now() : null;
return flagsDAO.markAsResolved(flagId, resolved, getHangarUserId(), resolvedAt);
return flagsDAO.markAsResolved(flagId, resolved, resolvedBy, resolvedAt);
}
public List<HangarProjectFlag> getFlags(String author, String slug) {

View File

@ -101,7 +101,7 @@ public class ProjectService extends HangarService {
if (Objects.equals(getHangarUserId(), userId)) {
return getHangarPrincipal();
}
return organizationService.getOrganizationTablesWithPermission(getHangarUserId(), Permission.CreateProject).stream().filter(ot -> ot.getUserId() == userId).findFirst().orElse(null);
return organizationService.getOrganizationTablesWithPermission(getHangarPrincipal().getId(), Permission.CreateProject).stream().filter(ot -> ot.getUserId() == userId).findFirst().orElse(null);
}
public ProjectOwner getProjectOwner(String userName) {