Skip to content

Commit

Permalink
feat(diary): make create comment interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hojagulyyev committed Dec 26, 2023
1 parent d148c0d commit e54d888
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
41 changes: 40 additions & 1 deletion apps/diaries/interactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from rp2.business_logic import COMMIT_MIN_LENGTH

from .models import Diary, DiaryCommit
from .models import Diary, DiaryCommit, DiaryComment
from .signals import diary_commit_created


Expand Down Expand Up @@ -59,3 +59,42 @@ def create_commit(request, diary_id: int):
diary_commit_created.send(sender=DiaryCommit, instance=diary_commit)

return redirect("diaries:detail_view", diary.id)


@login_required
def create_comment(request, diary_id: int):

# ===== DTO

body = request.POST.get("body", "").strip()
diary = Diary.objects.get(id=diary_id)

# ===== VALIDATION

if (
DiaryComment.objects
.filter(
diary=diary,
body=body,
created_datetime__date=datetime.date.today()
)
.exists()
):
messages.error(request, message="this message already exists for today")
return redirect(
f"{reverse(viewname='diaries:detail_view', kwargs={'id': diary.id})}"
f"?body={body}"
)

# ===== PROCESS

diary_comment = DiaryComment()
diary_comment.diary = diary
diary_comment.author = request.user.account
diary_comment.body = body
diary_comment.save()

diary.comments_last_read_by = request.user.account
diary.save(update_fields=["comments_last_read_by"])

return redirect("diaries:detail_view", diary.id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.6 on 2023-12-26 16:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('accounts', '0005_delete_xp'),
('diaries', '0004_alter_diary_account'),
]

operations = [
migrations.AddField(
model_name='diary',
name='comments_last_read_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.account'),
),
migrations.AlterField(
model_name='diarycomment',
name='diary',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='diaries.diary'),
),
]
3 changes: 2 additions & 1 deletion apps/diaries/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Diary(models.Model):

account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="diaries")
comments_last_read_by = models.ForeignKey(Account, on_delete=models.CASCADE, blank=True, null=True)
created_date = models.DateField(default=timezone.now)

class Meta:
Expand Down Expand Up @@ -37,7 +38,7 @@ def __str__(self):

class DiaryComment(models.Model):

diary = models.ForeignKey(Diary, on_delete=models.CASCADE)
diary = models.ForeignKey(Diary, on_delete=models.CASCADE, related_name="comments")
author = models.ForeignKey(Account, on_delete=models.CASCADE)
body = models.TextField()
created_datetime = models.DateTimeField(default=timezone.now)
Expand Down

0 comments on commit e54d888

Please sign in to comment.