EntityWithServiceClassResource.java

package io.github.jhipster.sample.web.rest;

/*-
 * #%L
 * Jhipster Sample Application
 * %%
 * Copyright (C) 2017 Osgiliath
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.sample.domain.EntityWithServiceClass;
import io.github.jhipster.sample.service.EntityWithServiceClassService;
import io.github.jhipster.sample.web.rest.errors.BadRequestAlertException;
import io.github.jhipster.sample.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing EntityWithServiceClass.
 */
@RestController
@RequestMapping("/api")
public class EntityWithServiceClassResource {

    private final Logger log = LoggerFactory.getLogger(EntityWithServiceClassResource.class);

    private static final String ENTITY_NAME = "entityWithServiceClass";

    private final EntityWithServiceClassService entityWithServiceClassService;

    public EntityWithServiceClassResource(EntityWithServiceClassService entityWithServiceClassService) {
        this.entityWithServiceClassService = entityWithServiceClassService;
    }

    /**
     * POST  /entity-with-service-classes : Create a new entityWithServiceClass.
     *
     * @param entityWithServiceClass the entityWithServiceClass to create
     * @return the ResponseEntity with status 201 (Created) and with body the new entityWithServiceClass, or with status 400 (Bad Request) if the entityWithServiceClass has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/entity-with-service-classes")
    @Timed
    public ResponseEntity<EntityWithServiceClass> createEntityWithServiceClass(@RequestBody EntityWithServiceClass entityWithServiceClass) throws URISyntaxException {
        log.debug("REST request to save EntityWithServiceClass : {}", entityWithServiceClass);
        if (entityWithServiceClass.getId() != null) {
            throw new BadRequestAlertException("A new entityWithServiceClass cannot already have an ID", ENTITY_NAME, "idexists");
        }
        EntityWithServiceClass result = entityWithServiceClassService.save(entityWithServiceClass);
        return ResponseEntity.created(new URI("/api/entity-with-service-classes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /entity-with-service-classes : Updates an existing entityWithServiceClass.
     *
     * @param entityWithServiceClass the entityWithServiceClass to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated entityWithServiceClass,
     * or with status 400 (Bad Request) if the entityWithServiceClass is not valid,
     * or with status 500 (Internal Server Error) if the entityWithServiceClass couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/entity-with-service-classes")
    @Timed
    public ResponseEntity<EntityWithServiceClass> updateEntityWithServiceClass(@RequestBody EntityWithServiceClass entityWithServiceClass) throws URISyntaxException {
        log.debug("REST request to update EntityWithServiceClass : {}", entityWithServiceClass);
        if (entityWithServiceClass.getId() == null) {
            return createEntityWithServiceClass(entityWithServiceClass);
        }
        EntityWithServiceClass result = entityWithServiceClassService.save(entityWithServiceClass);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, entityWithServiceClass.getId().toString()))
            .body(result);
    }

    /**
     * GET  /entity-with-service-classes : get all the entityWithServiceClasses.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of entityWithServiceClasses in body
     */
    @GetMapping("/entity-with-service-classes")
    @Timed
    public List<EntityWithServiceClass> getAllEntityWithServiceClasses() {
        log.debug("REST request to get all EntityWithServiceClasses");
        return entityWithServiceClassService.findAll();
        }

    /**
     * GET  /entity-with-service-classes/:id : get the "id" entityWithServiceClass.
     *
     * @param id the id of the entityWithServiceClass to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the entityWithServiceClass, or with status 404 (Not Found)
     */
    @GetMapping("/entity-with-service-classes/{id}")
    @Timed
    public ResponseEntity<EntityWithServiceClass> getEntityWithServiceClass(@PathVariable Long id) {
        log.debug("REST request to get EntityWithServiceClass : {}", id);
        EntityWithServiceClass entityWithServiceClass = entityWithServiceClassService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(entityWithServiceClass));
    }

    /**
     * DELETE  /entity-with-service-classes/:id : delete the "id" entityWithServiceClass.
     *
     * @param id the id of the entityWithServiceClass to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/entity-with-service-classes/{id}")
    @Timed
    public ResponseEntity<Void> deleteEntityWithServiceClass(@PathVariable Long id) {
        log.debug("REST request to delete EntityWithServiceClass : {}", id);
        entityWithServiceClassService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}