@ -330,6 +330,175 @@ nil "/users/~D"(mito:object-id user))))))))
` ( 303 ( :location
, ( format nil "/user/~D" id ) ) ) ) ) ) ) ) ) ) ) )
( defroute ( "/entries" :method :post ) ( &key method )
( cond ( ( string= "put" method )
( create-entry ( lack.request:request-body-parameters
ningle:*request* ) ) )
( t ` ( 401 ( ) ( , ( format nil "Unknown method ~S" method ) ) ) ) ) )
( defroute entries-index "/entries" ( &key id )
( with-connection ( db )
( let ( ( author ( when id ( mito:find-dao
'rails-to-caveman.model::user :id id ) ) ) )
( format t "[INFO] ~A" author )
( render "entries/index.html"
` ( :member , author
:user , ( current-user )
:news , ( articles-make 5 )
:blogs , ( entries :limit 5 )
:articles , ( articles-make 5 )
:entries , ( entries :author author )
,@ ( roles )
:token , ( token )
;; :member ,(rails-to-caveman.model::author-of entry))))))
) ) ) ) )
( defroute show-entry "/entries/:id" ( &key id )
( if ( null ( ignore-errors ( parse-integer id ) ) )
( myway:next-route )
( with-connection ( db )
( let ( ( entry ( mito:find-dao 'rails-to-caveman.model::entry :id id ) )
( entries ( entries :limit 5 ) ) )
( render "entries/show.html"
` ( ,@ ( roles )
:entry , entry
:token , ( token )
:user , ( current-user )
:entries , entries
:blogs , entries
:news , ( articles 5 )
:member , ( rails-to-caveman.model::author-of entry ) ) ) ) ) ) )
( defroute create-entry ( "/entries" :method :post ) ; :post was :put
( &key authenticity-token )
( step
( if ( not ( string= authenticity-token ( token ) ) )
' ( 401 ( ) ( "Denied" ) )
( with-connection ( db )
( multiple-value-bind ( entry errors )
( rails-to-caveman.model::validate-entry
( apply #' make-instance 'rails-to-caveman.model::entry
:user ( current-user )
( request-params
( lack.request:request-body-parameters
ningle:*request* ) ) ) )
( if errors ( render "entries/new.html"
` ( ,@ ( roles )
:entry , entry
:token , ( token )
:errors , errors
:user , ( current-user )
:blogs , ( entries :limit 5 )
:news , ( articles 5 ) ) )
( progn ( mito:save-dao entry )
( setf ( gethash :notice ningle:*session* )
"Stored" )
` ( 303 ( :location
, ( format nil "/entries/~D"
( mito:object-id entry ) ) ) ) ) ) ) ) ) ) )
( defroute ( "/entries/:id" :method :post ) ( &key method id )
( step
( cond ( ( string= "post" method )
( update-entry ( acons "ID" id
( lack.request:request-body-parameters
ningle:*request* ) ) ) )
( ( string= "delete" method )
( destroy-entry ( acons "ID" id
( lack.request:request-body-parameters
ningle:*request* ) ) ) )
( t ` ( 401 ( ) ( , ( format nil "Unknown method ~S" method ) ) ) ) ) ) )
( defun update-entry ( request )
( destructuring-bind ( &key
authenticity-token
id
title
body
posted-year
posted-month
posted-day
posted-hour
posted-min
&allow-other-keys )
( request-params request )
( if ( not ( string= authenticity-token ( token ) ) )
' ( 401 ( ) ( "Denied" ) )
( if ( null ( ignore-errors ( parse-integer id ) ) )
( myway:next-route )
( with-connection ( db )
( let ( ( entry ( mito:find-dao
'rails-to-caveman.model::entry :id id ) ) )
( setf ( rails-to-caveman.model::title-of entry ) title
( rails-to-caveman.model::body-of entry ) body
( rails-to-caveman.model::date-posted-of entry )
( format nil "~A-~A-~AT~A:~A:00"
posted-year
posted-month
posted-day
posted-hour
posted-min ) )
( multiple-value-bind ( entry errors )
( rails-to-caveman.model::validate-entry entry )
( if errors ( render "entries/edit.html"
` ( ,@ ( roles )
:member , ( rails-to-caveman.model::author-of entry )
:user , ( current-user )
:token , ( token )
:entry , entry
:news , ( articles 5 )
:blogs , ( entries :limit 5 )
:errors , errors ) )
( progn ( mito:save-dao entry )
( setf ( gethash :notice ningle:*session* ) "Updated" )
` ( 303 ( :location
, ( format nil "/entries/~D"
( mito:object-id entry ) ) ) ) ) ) ) ) ) ) ) ) )
( defroute "/entries/new" ( &key )
( step
( render "entries/new.html"
` ( ,@ ( roles )
:token , ( token )
:entry , ( make-instance 'rails-to-caveman.model::entry
:date-posted
( local-time:now ) )
:user , ( current-user )
:blogs , ( entries :limit 5 )
:news , ( articles 5 ) ) ) ) )
( defroute "/entries/:id/edit" ( &key id )
( if ( null ( ignore-errors ( parse-integer id ) ) )
( myway:next-route )
( render "entries/edit.html"
` ( ,@ ( roles )
:entry , ( with-connection ( db )
( mito:find-dao 'rails-to-caveman.model::entry :id id ) )
:user , ( current-user )
:blogs , ( entries :limit 5 )
:news , ( articles 5 ) ) ) ) )
( defroute destroy-entry ( "/entries/:id" :method :delete )
( &key authenticity-token id )
( if ( not ( string= authenticity-token ( token ) ) )
' ( 401 ( ) ( "Denied" ) )
( with-connection ( db )
( if ( null ( ignore-errors ( parse-integer id ) ) )
( myway:next-route )
( let ( ( entry ( mito:find-dao 'rails-to-caveman.model::entry :id id ) ) )
( if ( null entry )
` ( 401 ( ) ( , ( format nil "Entry id ~A is not exist" id ) ) )
( progn ( mito:delete-dao entry )
( setf ( gethash :notice ningle:*session* ) "Deleted" )
` ( 303 ( :location
, ( format nil "/user/~D/entries"
( mito:object-id ( current-user ) ) ) ) ) ) ) ) ) ) ) )
( defroute "/user/:id/entries" ( &key id )
( entries-index ( acons "ID" id
( lack.request:request-body-parameters
ningle:*request* ) ) ) )
( defroute "/account" ( )
( if ( not ( hermetic:logged-in-p ) )
' ( 401 ( ) )
@ -558,7 +727,7 @@ nil "/users/~D"(mito:object-id user))))))))
,@ ( roles )
:token , ( token ) ) ) )
( defroute new-article ( "/articles" :method :put )
( defroute new-article ( "/articles" :method :put ) ; :put need to be put?
( &key authenticity-token no-expiration-p )
( if ( not ( string= authenticity-token ( token ) ) )
' ( 401 ( ) ( "Denied" ) )
@ -974,3 +1143,26 @@ nil "/users/~D"(mito:object-id user))))))))
;; (:= :only-member your-app.model::+false+)))
;; (sxql:order-by(:desc :date-released))
;; (sxql:limit n)))))
( defun entries ( &key ( logged-in-p ( hermetic:logged-in-p ) )
( user ( and logged-in-p ( current-user ) ) )
author limit )
( with-connection ( db )
( mito:select-dao 'rails-to-caveman.model::entry
( sxql:where ( trivia:match* ( logged-in-p author )
( ( nil nil ) ` ( := "public" :status ) )
( ( nil _ )
` ( :and ( := , ( mito:object-id author ) :user-id )
( := "public" :status ) ) )
( ( _ nil ) ` ( :or ( := "public" :status )
( := "member-only" :status )
( :and ( := "draft" :stats )
( := , ( mito:object-id user )
:user-id ) ) ) )
( ( _ _ ) ( if ( mito:object= user author )
` ( := , ( mito:object-id user )
:user-id )
` ( :and ( := , ( mito:object-id author ) :user-id )
( :or ( := "public" :status )
( := "member-only" :status ) ) ) ) ) ) )
( sxql:order-by ( :desc :date-posted ) ) ( when limit ( sxql:limit limit ) ) ) ) )