mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
refactor: update AdventureSerializer to handle visits data more gracefully and remove visits from request body in AdventureCard
This commit is contained in:
parent
6651557738
commit
57e367d112
2 changed files with 21 additions and 19 deletions
|
@ -116,7 +116,7 @@ class AdventureSerializer(CustomModelSerializer):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
visits_data = validated_data.pop('visits', [])
|
visits_data = validated_data.pop('visits', None)
|
||||||
category_data = validated_data.pop('category', None)
|
category_data = validated_data.pop('category', None)
|
||||||
print(category_data)
|
print(category_data)
|
||||||
adventure = Adventure.objects.create(**validated_data)
|
adventure = Adventure.objects.create(**validated_data)
|
||||||
|
@ -131,6 +131,7 @@ class AdventureSerializer(CustomModelSerializer):
|
||||||
return adventure
|
return adventure
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
|
has_visits = 'visits' in validated_data
|
||||||
visits_data = validated_data.pop('visits', [])
|
visits_data = validated_data.pop('visits', [])
|
||||||
category_data = validated_data.pop('category', None)
|
category_data = validated_data.pop('category', None)
|
||||||
|
|
||||||
|
@ -142,24 +143,25 @@ class AdventureSerializer(CustomModelSerializer):
|
||||||
instance.category = category
|
instance.category = category
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
||||||
current_visits = instance.visits.all()
|
if has_visits:
|
||||||
current_visit_ids = set(current_visits.values_list('id', flat=True))
|
current_visits = instance.visits.all()
|
||||||
|
current_visit_ids = set(current_visits.values_list('id', flat=True))
|
||||||
|
|
||||||
updated_visit_ids = set()
|
updated_visit_ids = set()
|
||||||
for visit_data in visits_data:
|
for visit_data in visits_data:
|
||||||
visit_id = visit_data.get('id')
|
visit_id = visit_data.get('id')
|
||||||
if visit_id and visit_id in current_visit_ids:
|
if visit_id and visit_id in current_visit_ids:
|
||||||
visit = current_visits.get(id=visit_id)
|
visit = current_visits.get(id=visit_id)
|
||||||
for attr, value in visit_data.items():
|
for attr, value in visit_data.items():
|
||||||
setattr(visit, attr, value)
|
setattr(visit, attr, value)
|
||||||
visit.save()
|
visit.save()
|
||||||
updated_visit_ids.add(visit_id)
|
updated_visit_ids.add(visit_id)
|
||||||
else:
|
else:
|
||||||
new_visit = Visit.objects.create(adventure=instance, **visit_data)
|
new_visit = Visit.objects.create(adventure=instance, **visit_data)
|
||||||
updated_visit_ids.add(new_visit.id)
|
updated_visit_ids.add(new_visit.id)
|
||||||
|
|
||||||
visits_to_delete = current_visit_ids - updated_visit_ids
|
visits_to_delete = current_visit_ids - updated_visit_ids
|
||||||
instance.visits.filter(id__in=visits_to_delete).delete()
|
instance.visits.filter(id__in=visits_to_delete).delete()
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,7 @@
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ collection: null, visits: adventure.visits })
|
body: JSON.stringify({ collection: null })
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
addToast('info', `${$t('adventures.collection_remove_success')}`);
|
addToast('info', `${$t('adventures.collection_remove_success')}`);
|
||||||
|
@ -97,7 +97,7 @@
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ collection: collectionId, visits: adventure.visits })
|
body: JSON.stringify({ collection: collectionId })
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
console.log('Adventure linked to collection');
|
console.log('Adventure linked to collection');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue