|
| 1 | +import uuid |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import strawberry |
| 5 | +import strawberry_django |
| 6 | +from strawberry.types import Info |
| 7 | +from strawberry_django.pagination import OffsetPaginationInput |
| 8 | + |
| 9 | +from api.models import SDG |
| 10 | +from api.types.type_sdg import SDGFilter, SDGOrder, TypeSDG |
| 11 | + |
| 12 | + |
| 13 | +@strawberry.input |
| 14 | +class SDGInput: |
| 15 | + name: str |
| 16 | + code: str |
| 17 | + description: Optional[str] = None |
| 18 | + |
| 19 | + |
| 20 | +@strawberry_django.partial(SDG) |
| 21 | +class SDGInputPartial: |
| 22 | + id: uuid.UUID |
| 23 | + name: Optional[str] = None |
| 24 | + code: Optional[str] = None |
| 25 | + description: Optional[str] = None |
| 26 | + slug: Optional[str] = None |
| 27 | + |
| 28 | + |
| 29 | +@strawberry.type(name="Query") |
| 30 | +class Query: |
| 31 | + sdgs: list[TypeSDG] = strawberry_django.field() |
| 32 | + |
| 33 | + @strawberry_django.field |
| 34 | + def sdg(self, info: Info, id: uuid.UUID) -> Optional[TypeSDG]: |
| 35 | + """Get SDG by ID.""" |
| 36 | + try: |
| 37 | + sdg = SDG.objects.get(id=id) |
| 38 | + return TypeSDG.from_django(sdg) |
| 39 | + except SDG.DoesNotExist: |
| 40 | + raise ValueError(f"SDG with ID {id} does not exist.") |
| 41 | + |
| 42 | + @strawberry_django.field( |
| 43 | + filters=SDGFilter, |
| 44 | + pagination=True, |
| 45 | + order=SDGOrder, |
| 46 | + ) |
| 47 | + def all_sdgs( |
| 48 | + self, |
| 49 | + info: Info, |
| 50 | + filters: Optional[SDGFilter] = strawberry.UNSET, |
| 51 | + pagination: Optional[OffsetPaginationInput] = strawberry.UNSET, |
| 52 | + order: Optional[SDGOrder] = strawberry.UNSET, |
| 53 | + ) -> list[TypeSDG]: |
| 54 | + """Get all SDGs.""" |
| 55 | + queryset = SDG.objects.all() |
| 56 | + |
| 57 | + # Apply filters if provided |
| 58 | + if filters is not strawberry.UNSET: |
| 59 | + queryset = strawberry_django.filters.apply(filters, queryset, info) |
| 60 | + |
| 61 | + # Apply ordering if provided |
| 62 | + if order is not strawberry.UNSET: |
| 63 | + queryset = strawberry_django.ordering.apply(order, queryset, info) |
| 64 | + |
| 65 | + # Apply pagination if provided |
| 66 | + if pagination is not strawberry.UNSET: |
| 67 | + queryset = strawberry_django.pagination.apply(pagination, queryset) |
| 68 | + |
| 69 | + return [TypeSDG.from_django(instance) for instance in queryset] |
| 70 | + |
| 71 | + |
| 72 | +@strawberry.type |
| 73 | +class Mutation: |
| 74 | + @strawberry_django.mutation(handle_django_errors=True) |
| 75 | + def create_sdg(self, info: Info, input: SDGInput) -> TypeSDG: |
| 76 | + """Create a new SDG.""" |
| 77 | + sdg = SDG( |
| 78 | + name=input.name, |
| 79 | + code=input.code, |
| 80 | + description=input.description, |
| 81 | + ) |
| 82 | + sdg.save() |
| 83 | + return TypeSDG.from_django(sdg) |
| 84 | + |
| 85 | + @strawberry_django.mutation(handle_django_errors=True) |
| 86 | + def update_sdg(self, info: Info, input: SDGInputPartial) -> Optional[TypeSDG]: |
| 87 | + """Update an existing SDG.""" |
| 88 | + try: |
| 89 | + sdg = SDG.objects.get(id=input.id) |
| 90 | + |
| 91 | + # Update fields if provided |
| 92 | + if input.name is not None: |
| 93 | + sdg.name = input.name |
| 94 | + if input.code is not None: |
| 95 | + sdg.code = input.code |
| 96 | + if input.description is not None: |
| 97 | + sdg.description = input.description |
| 98 | + if input.slug is not None: |
| 99 | + sdg.slug = input.slug |
| 100 | + |
| 101 | + sdg.save() |
| 102 | + return TypeSDG.from_django(sdg) |
| 103 | + except SDG.DoesNotExist: |
| 104 | + raise ValueError(f"SDG with ID {input.id} does not exist.") |
| 105 | + |
| 106 | + @strawberry_django.mutation(handle_django_errors=False) |
| 107 | + def delete_sdg(self, info: Info, sdg_id: uuid.UUID) -> bool: |
| 108 | + """Delete an SDG.""" |
| 109 | + try: |
| 110 | + sdg = SDG.objects.get(id=sdg_id) |
| 111 | + sdg.delete() |
| 112 | + return True |
| 113 | + except SDG.DoesNotExist: |
| 114 | + raise ValueError(f"SDG with ID {sdg_id} does not exist.") |
0 commit comments