6767)
6868from ..data import VersionResponse
6969from ..middleware import BroodAuthMiddleware
70- from .models import Journal , JournalEntryTag
70+ from .models import Journal , JournalEntryLock , JournalEntryTag
7171from . import search
7272from ..utils .settings import (
7373 DEFAULT_JOURNALS_ES_INDEX ,
@@ -865,10 +865,10 @@ async def create_journal_entry(
865865 es_index = journal .search_index
866866
867867 try :
868- journal_entry = await actions .create_journal_entry (
869- db_session ,
870- creation_request ,
871- user_group_id_list = request . state . user_group_id_list ,
868+ journal_entry , entry_lock = await actions .create_journal_entry (
869+ db_session = db_session ,
870+ journal = journal ,
871+ entry_request = creation_request ,
872872 locked_by = request .state .user_id ,
873873 )
874874 except actions .JournalNotFound :
@@ -917,7 +917,7 @@ async def create_journal_entry(
917917 updated_at = journal_entry .updated_at ,
918918 context_url = journal_entry .context_url ,
919919 context_type = journal_entry .context_type ,
920- locked_by = journal_entry .locked_by ,
920+ locked_by = entry_lock .locked_by ,
921921 )
922922
923923
@@ -1081,7 +1081,11 @@ async def get_entry(
10811081 )
10821082
10831083 try :
1084- journal_entry , tag_objects = await actions .get_journal_entry_with_tags (
1084+ (
1085+ journal_entry ,
1086+ tag_objects ,
1087+ entry_lock ,
1088+ ) = await actions .get_journal_entry_with_tags (
10851089 db_session = db_session , journal_entry_id = entry_id
10861090 )
10871091 except actions .EntryNotFound :
@@ -1106,7 +1110,7 @@ async def get_entry(
11061110 updated_at = journal_entry .updated_at ,
11071111 context_url = journal_entry .context_url ,
11081112 context_type = journal_entry .context_type ,
1109- locked_by = journal_entry .locked_by ,
1113+ locked_by = None if entry_lock is None else entry_lock .locked_by ,
11101114 )
11111115
11121116
@@ -1198,7 +1202,11 @@ async def update_entry_content(
11981202 es_index = journal .search_index
11991203
12001204 try :
1201- journal_entry , tag_objects = await actions .get_journal_entry_with_tags (
1205+ (
1206+ journal_entry ,
1207+ tag_objects ,
1208+ entry_lock ,
1209+ ) = await actions .get_journal_entry_with_tags (
12021210 db_session = db_session , journal_entry_id = entry_id
12031211 )
12041212 if journal_entry is None :
@@ -1211,22 +1219,26 @@ async def update_entry_content(
12111219 logger .error (f"Error listing journal entries: { str (e )} " )
12121220 raise HTTPException (status_code = 500 )
12131221
1214- if (
1215- journal_entry .locked_by is not None
1216- and journal_entry .locked_by != request .state .user_id
1217- ):
1222+ if entry_lock is not None and entry_lock .locked_by != request .state .user_id :
12181223 return JournalEntryContent (
12191224 title = journal_entry .title ,
12201225 content = journal_entry .content ,
12211226 tags = [tag .tag for tag in tag_objects ],
1222- locked_by = journal_entry .locked_by ,
1227+ locked_by = entry_lock .locked_by ,
12231228 )
12241229
1225- journal_entry .title = api_request .title
1226- journal_entry .content = api_request .content
1227- journal_entry .locked_by = request .state .user_id
1228- db_session .add (journal_entry )
1229- db_session .commit ()
1230+ try :
1231+ journal_entry , entry_lock = await actions .update_journal_entry (
1232+ db_session = db_session ,
1233+ new_title = api_request .title ,
1234+ new_content = api_request .content ,
1235+ locked_by = request .state .user_id ,
1236+ journal_entry = journal_entry ,
1237+ entry_lock = entry_lock ,
1238+ )
1239+ except Exception as e :
1240+ logger .error (f"Error updating journal entry: { str (e )} " )
1241+ raise HTTPException (status_code = 500 )
12301242
12311243 updated_tag_objects : List [JournalEntryTag ] = []
12321244 try :
@@ -1286,7 +1298,7 @@ async def update_entry_content(
12861298 title = journal_entry .title ,
12871299 content = journal_entry .content ,
12881300 tags = tags ,
1289- locked_by = request . state . user_id ,
1301+ locked_by = entry_lock . locked_by ,
12901302 )
12911303
12921304
@@ -1313,27 +1325,31 @@ async def delete_entry_lock(
13131325 {JournalEntryScopes .UPDATE },
13141326 )
13151327 try :
1316- journal_entry , tag_objects = await actions .get_journal_entry_with_tags (
1328+ (
1329+ journal_entry ,
1330+ tag_objects ,
1331+ entry_lock ,
1332+ ) = await actions .get_journal_entry_with_tags (
13171333 db_session = db_session , journal_entry_id = entry_id
13181334 )
13191335 if journal_entry is None :
13201336 raise actions .EntryNotFound (
13211337 f"Entry not found with ID={ entry_id } in journal with ID={ journal_id } "
13221338 )
1323- await actions .delete_journal_entry_lock (
1324- db_session = db_session ,
1325- journal_entry_id = entry_id ,
1326- )
1339+ if entry_lock is not None :
1340+ db_session .delete (entry_lock )
1341+ db_session .commit ()
13271342 except actions .EntryNotFound :
13281343 raise HTTPException (status_code = 404 , detail = "Entry not found" )
1329- except Exception :
1344+ except Exception as e :
1345+ logger .error (f"Error listing journal entries: { str (e )} " )
13301346 raise HTTPException (status_code = 500 )
13311347
13321348 return JournalEntryContent (
13331349 title = journal_entry .title ,
13341350 content = journal_entry .content ,
13351351 tags = [tag .tag for tag in tag_objects ],
1336- locked_by = journal_entry . locked_by ,
1352+ locked_by = None ,
13371353 )
13381354
13391355
0 commit comments