@ -11,10 +11,8 @@ import com.sun.net.httpserver.HttpExchange;
@@ -11,10 +11,8 @@ import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.cal.api.Appointment ;
import de.srsoftware.cal.api.Link ;
import de.srsoftware.cal.db.Database ;
import de.srsoftware.tools.* ;
import de.srsoftware.tools.Error ;
import de.srsoftware.tools.PathHandler ;
import de.srsoftware.tools.Payload ;
import de.srsoftware.tools.Result ;
import java.io.IOException ;
import java.sql.SQLException ;
import java.time.LocalDate ;
@ -22,6 +20,7 @@ import java.time.LocalDateTime;
@@ -22,6 +20,7 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter ;
import java.util.List ;
import java.util.Map ;
import java.util.Optional ;
import org.json.JSONObject ;
public class ApiHandler extends PathHandler {
@ -44,7 +43,37 @@ public class ApiHandler extends PathHandler {
@@ -44,7 +43,37 @@ public class ApiHandler extends PathHandler {
case "/tags" - > listTags ( ex , params ) ;
default - > PathHandler . notFound ( ex ) ;
} ;
}
@Override
public boolean doDelete ( String path , HttpExchange ex ) throws IOException {
var params = queryParam ( ex ) ;
return switch ( path ) {
case "/event" - > delete ( ex , params ) ;
default - > PathHandler . notFound ( ex ) ;
} ;
}
private boolean delete ( HttpExchange ex , Map < String , String > params ) throws IOException {
var aid = params . get ( AID ) ;
if ( aid = = null ) return sendContent ( ex , Error . of ( "Missing appointment id" ) ) ;
long id = 0 ;
try {
id = Long . parseLong ( aid ) ;
} catch ( Exception e ) {
return sendContent ( ex , Error . format ( "%s is not a valid id!" , aid ) ) ;
}
var json = json ( ex ) ;
var title = json . has ( TITLE ) ? nullIfEmpty ( json . getString ( TITLE ) ) :
null ;
if ( title = = null ) return sendContent ( ex , Error . of ( "Missing appointment title" ) ) ;
var res = db . loadEvent ( id ) ;
if ( res . optional ( ) . isEmpty ( ) ) return sendContent ( ex , res ) ;
var event = res . optional ( ) . get ( ) ;
if ( ! title . equals ( event . title ( ) ) ) return sendContent ( ex , Error . of ( "Title mismatch!" ) ) ;
var del = db . removeAppointment ( id ) ;
if ( del . optional ( ) . isEmpty ( ) ) return sendContent ( ex , del ) ;
return sendContent ( ex , Map . of ( "deleted" , del . optional ( ) . get ( ) ) ) ;
}
@Override
@ -58,16 +87,22 @@ public class ApiHandler extends PathHandler {
@@ -58,16 +87,22 @@ public class ApiHandler extends PathHandler {
// spotless:off
private boolean editEvent ( HttpExchange ex ) throws IOException {
var json = json ( ex ) ;
Optional < Appointment > existingAppointment = Optional . empty ( ) ;
Long aid = json . has ( AID ) ? json . getLong ( AID ) : null ;
if ( aid ! = null ) { // load appointment by aid
existingAppointment = db . loadEvent ( aid ) . optional ( ) ;
} else { // try to load appointment by location @ time
var location = json . has ( LOCATION ) ? json . getString ( LOCATION ) : null ;
var start = json . has ( START ) ? LocalDateTime . parse ( json . getString ( START ) ) : null ;
if ( allSet ( location , start ) ) {
var existingAppointment = db . loadEvent ( location , start ) . optional ( ) ;
existingAppointment = db . loadEvent ( location , start ) . optional ( ) ;
}
}
if ( existingAppointment . isPresent ( ) ) {
var event = existingAppointment . get ( ) ;
json . put ( AID , event . id ( ) ) ;
return update ( ex , toEvent ( json ) ) ;
}
}
return createEvent ( ex , json ) ;
}