NEW API Link

Wael5 years ago

Hi I am trying to add new table and create new api, i have searched to forum and followed some,
so far i have created /api/form
and created the model with getters and setters
created the url path for the API but not sure how can i get this to save into Database.
i think I am missing the service that connect the db with model.
I am new to Java, i used php previously, can anyone advice me how can i get this api path to save data to database and i will contribute this new feature through github.

/*
 * Copyright 2013 - 2018 Anton Tananaev (anton@traccar.org)
 *
 * 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.
 */
package org.traccar.model;


import java.util.Date;

public class Form extends ExtendedModel{

    private long id;
    private String name;
    private Date startDate;
    private Date endDate;
    private long lat;
    private long longi;
    private Date serverTime;

    public Form(String name, Date startDate, Date endDate, long lat, long longi){
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.lat = lat;
        this.longi = longi;
        this.serverTime = new Date();
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public long getLat() {
        return lat;
    }

    public void setLat(long lat) {
        this.lat = lat;
    }

    public long getLongi() {
        return longi;
    }

    public void setLongi(long longi) {
        this.longi = longi;
    }

    public Date getServerTime() {
        return serverTime;
    }

    public void setServerTime(Date serverTime) {
        this.serverTime = serverTime;
    }
}
package org.traccar.api.resource;

import org.traccar.api.BaseResource;
import org.traccar.model.Form;


import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;


@Path("form")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)

public class FormResource extends BaseResource {

    @javax.ws.rs.core.Context
    private HttpServletRequest request;
    @PermitAll
    @GET
    public String get() {
        return "Get Method Test";
    }

    @PermitAll
    @POST
    public String postForm(@FormParam("name") String name, @FormParam("startDate") String startDate
            , @FormParam("endDate") String endDate, @FormParam("lat") String lat, @FormParam("longi") String longi,
                           @FormParam("serverTime") String serverTime){
        String result = "Name: "+ name + " Start Date: "+ startDate + " Finish Date: "+ endDate + " lat " + lat + " longi" + longi + " serverTime :" + serverTime;
        Form form = new Form(name,startDate,endDate, lat, longi);
        return result;
    }

}

thanks in advance.