All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blossomproject.ui.api.administration.GroupsApiController Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.blossomproject.ui.api.administration;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.blossomproject.core.common.dto.AbstractDTO;
import com.blossomproject.core.common.search.SearchEngineImpl;
import com.blossomproject.core.group.GroupCreateForm;
import com.blossomproject.core.group.GroupDTO;
import com.blossomproject.core.group.GroupService;
import com.blossomproject.core.group.GroupUpdateForm;
import com.blossomproject.ui.stereotype.BlossomApiController;
import java.util.Map;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Maël Gargadennnec on 04/05/2017.
 */
@BlossomApiController
@RequestMapping("/administration/groups")
public class GroupsApiController {

  private final GroupService groupService;
  private final SearchEngineImpl searchEngine;

  public GroupsApiController(GroupService groupService,
    SearchEngineImpl searchEngine) {
    this.groupService = groupService;
    this.searchEngine = searchEngine;
  }

  @GetMapping
  @PreAuthorize("hasAuthority('administration:groups:read')")
  public Page list(
    @RequestParam(value = "q", required = false) String q, @PageableDefault(size = 25) Pageable pageable) {

    if (Strings.isNullOrEmpty(q)) {
      return this.groupService.getAll(pageable);
    }
    return this.searchEngine.search(q, pageable).getPage();
  }

  @PostMapping
  @PreAuthorize("hasAuthority('administration:groups:create')")
  public ResponseEntity create(@NotNull @Valid @RequestBody GroupCreateForm groupCreateForm)
    throws Exception {
    Preconditions.checkArgument(groupCreateForm != null);
    return new ResponseEntity<>(groupService.create(groupCreateForm), HttpStatus.CREATED);
  }

  @GetMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:groups:read')")
  public ResponseEntity get(@PathVariable Long id) {
    Preconditions.checkArgument(id != null);
    GroupDTO group = groupService.getOne(id);
    if (group == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
      return new ResponseEntity<>(group, HttpStatus.OK);
    }
  }

  @PutMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:groups:write')")
  public ResponseEntity update(@PathVariable Long id,
    @Valid @RequestBody GroupUpdateForm groupUpdateForm) {
    Preconditions.checkArgument(id != null);
    GroupDTO group = groupService.getOne(id);
    if (group == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
      return new ResponseEntity<>(groupService.update(id, groupUpdateForm), HttpStatus.OK);
    }
  }

  @DeleteMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:groups:delete')")
  public ResponseEntity, Long>> delete(@PathVariable Long id,
    @RequestParam(value = "force", defaultValue = "false", required = false) boolean force) {
    GroupDTO group = this.groupService.getOne(id);
    if (group == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    Optional, Long>> result = this.groupService.delete(group, force);
    if (!result.isPresent() || result.get().isEmpty()) {
      return new ResponseEntity<>(HttpStatus.OK);
    } else {
      return new ResponseEntity<>(result.get(), HttpStatus.CONFLICT);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy