Skip to content

Commit

Permalink
Merge pull request #15 from githubuniverseworkshops/arilivigni/self-s…
Browse files Browse the repository at this point in the history
…ervice-prompting

Arilivigni/self service prompting
  • Loading branch information
bryantson authored Oct 26, 2024
2 parents 3ced6b5 + eb73dae commit 14f56c6
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/5_BackendSettings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,49 @@ class Workout(models.Model):

from rest_framework import serializers
from .models import User, Team, Activity, Leaderboard, Workout
from bson import ObjectId

class ObjectIdField(serializers.Field):
def to_representation(self, value):
return str(value)

def to_internal_value(self, data):
return ObjectId(data)

class UserSerializer(serializers.ModelSerializer):
_id = ObjectIdField()

class Meta:
model = User
fields = '__all__'

class TeamSerializer(serializers.ModelSerializer):
_id = ObjectIdField()
members = UserSerializer(many=True)

class Meta:
model = Team
fields = '__all__'

class ActivitySerializer(serializers.ModelSerializer):
_id = ObjectIdField()
user = ObjectIdField()

class Meta:
model = Activity
fields = '__all__'

class LeaderboardSerializer(serializers.ModelSerializer):
_id = ObjectIdField()
user = ObjectIdField()

class Meta:
model = Leaderboard
fields = '__all__'

class WorkoutSerializer(serializers.ModelSerializer):
_id = ObjectIdField()

class Meta:
model = Workout
fields = '__all__'
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
73 changes: 73 additions & 0 deletions docs/7_CodespaceDjangoRESTFramework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Type the following prompt in GitHub Copilot Chat:

> NOTE: Replace `[REPLACE-THIS-WITH-YOUR-CODESPACE-NAME]` with the name of your codespace
```text
Let's do the following step by step
Expand Down Expand Up @@ -37,4 +39,75 @@ HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accep
}
```

![update for codespace endpoint prompt](./7_1_CopilotPromptCodespaceEndpointPrompt.png)
![update for codespace endpoint step 1](./7_2_CopilotPromptCodespaceEndpointStep1.png)
![update for codespace endpoint step 2](./7_2_CopilotPromptCodespaceEndpointStep2.png)

## Update to views.py

```python
# FILE: octofit_tracker/views.py

from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from .serializers import UserSerializer, TeamSerializer, ActivitySerializer, LeaderboardSerializer, WorkoutSerializer
from .models import User, Team, Activity, Leaderboard, Workout

@api_view(['GET'])
def api_root(request, format=None):
base_url = 'http://[REPLACE-THIS-WITH-YOUR-CODESPACE-NAME]-8000.app.github.dev/'
return Response({
'users': base_url + 'api/users/?format=api',
'teams': base_url + 'api/teams/?format=api',
'activity': base_url + 'api/activity/?format=api',
'leaderboard': base_url + 'api/leaderboard/?format=api',
'workouts': base_url + 'api/workouts/?format=api'
})

class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer

class TeamViewSet(viewsets.ModelViewSet):
queryset = Team.objects.all()
serializer_class = TeamSerializer

class ActivityViewSet(viewsets.ModelViewSet):
queryset = Activity.objects.all()
serializer_class = ActivitySerializer

class LeaderboardViewSet(viewsets.ModelViewSet):
queryset = Leaderboard.objects.all()
serializer_class = LeaderboardSerializer

class WorkoutViewSet(viewsets.ModelViewSet):
queryset = Workout.objects.all()
serializer_class = WorkoutSerializer
```

## Run the server via manage.py

```bash
python manage.py runserver
```

## Django REST API framwork :: users, teams, activity, leaderboard, workouts

![Django REST API framwork :: users, teams, activity, leaderboard, workouts](<Rewatch GIF Recording - 2024-10-25 at 7.49.58 PM.gif>)

## GitHub Copilot Chat commands to help debug issues

```text
/help
#selection - The current selection in the active editor
#codebase - Searches through the codebase and pulls out relevant information for the query.
#editor - The visible source code in the active editor
#terminalLastCommand - The active terminal's last run command
#terminalSelection - The active terminal's selection
#file - Choose a file in the workspace
```

[:arrow_backward: Previous: Populate DB data](../6_PopulateDBwData) | [Next: What's next :arrow_forward:](../8_WhatsNext/README.md)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 14f56c6

Please sign in to comment.