Newer
Older
vue-demo / service / src / main / java / web / StudentResource.java
Mark George on 27 Aug 2021 1 KB Add project
package web;

import dao.StudentDao;
import domain.Student;
import io.jooby.Jooby;
import io.jooby.MediaType;
import io.jooby.StatusCode;

/**
 *
 * @author Mark George
 */
public class StudentResource extends Jooby {

	public StudentResource(StudentDao dao) {

		path("/api/students", () -> {

			post("", ctx -> {
				Student student = ctx.body().to(Student.class);
				dao.addStudent(student);
				System.out.println("Created " + student);
				return ctx.send(StatusCode.CREATED);
			});

			get("", ctx -> dao.getStudents());

			get("/{id}", ctx -> dao.getStudentById(ctx.path("id").value()));

			delete("/{id}", ctx -> {
				String id = ctx.path("id").value();
				dao.removeStudent(id);
				System.out.println("Removed " + id);
				return ctx.send(StatusCode.NO_CONTENT);
			});

			put("/{id}", ctx -> {
				String id = ctx.path("id").value();
				Student student = ctx.body().to(Student.class);
				dao.replaceStudent(id, student);
				System.out.println("Replaced " + id + " with " + student);
				return ctx.send(StatusCode.NO_CONTENT);
			});

		}).produces(MediaType.json);

	}

}