Skip to content

Commit

Permalink
REFACTOR: REST API endpoints and integration in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
pymike00 committed Apr 23, 2023
1 parent b4640c0 commit fdfc6da
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/questions/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
urlpatterns = [
path("", include(router.urls)),

path("questions/<slug:slug>/answers/",
path("questions-answers/<slug:slug>/",
qv.AnswerListAPIView.as_view(),
name="answer-list"),

path("questions/<slug:slug>/answer/",
path("questions-new-answer/<slug:slug>/",
qv.AnswerCreateAPIView.as_view(),
name="answer-create"),

path("answers/<uuid:uuid>/",
path("answers-detail/<uuid:uuid>/",
qv.AnswerRUDAPIView.as_view(),
name="answer-detail"),

path("answers/<uuid:uuid>/like/",
path("answers-like/<uuid:uuid>/",
qv.AnswerLikeAPIView.as_view(),
name="answer-like")
]
3 changes: 2 additions & 1 deletion src/vite-frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
import NavbarComponent from "@/components/Navbar.vue";
export default {
Expand All @@ -18,7 +19,7 @@ export default {
async setUserInfo() {
// add the username of the current user to localStorage
try {
const response = await axios.get("/auth/users/me/");
const response = await axios.get(endpoints["usersDetail"]);
const requestUser = response.data["username"];
window.localStorage.setItem("username", requestUser);
} catch (error) {
Expand Down
14 changes: 14 additions & 0 deletions src/vite-frontend/src/common/endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const baseAcademyEndpoint = "/api/v1/";

const endpoints = {
questionsCRUD: `${baseAcademyEndpoint}questions/`,
questionsAnswersList: `${baseAcademyEndpoint}questions-answers/`,
questionsNewAnswer: `${baseAcademyEndpoint}questions-new-answer/`,

answersDetail: `${baseAcademyEndpoint}answers-detail/`,
answersLike: `${baseAcademyEndpoint}answers-like/`,

usersDetail: "/auth/users/me/"
};

export { endpoints };
5 changes: 3 additions & 2 deletions src/vite-frontend/src/components/Answer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
export default {
name: "AnswerComponent",
Expand Down Expand Up @@ -63,7 +64,7 @@ export default {
async likeAnswer() {
this.userLikedAnswer = true;
this.likesCounter += 1;
const endpoint = `/api/v1/answers/${this.answer.uuid}/like/`;
const endpoint = `${endpoints["answersLike"]}${this.answer.uuid}/`;
try {
await axios.post(endpoint);
} catch (error) {
Expand All @@ -74,7 +75,7 @@ export default {
async unLikeAnswer() {
this.userLikedAnswer = false;
this.likesCounter -= 1;
const endpoint = `/api/v1/answers/${this.answer.uuid}/like/`;
const endpoint = `${endpoints["answersLike"]}${this.answer.uuid}/`;
try {
await axios.delete(endpoint);
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion src/vite-frontend/src/components/QuestionActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
export default {
name: "QuestionActions",
Expand All @@ -31,7 +32,7 @@ export default {
},
methods: {
async deleteQuestion() {
const endpoint = `/api/v1/questions/${this.slug}/`;
const endpoint = `${endpoints["questionsCRUD"]}${this.slug}/`;
try {
await axios.delete(endpoint);
this.$router.push({
Expand Down
5 changes: 3 additions & 2 deletions src/vite-frontend/src/views/AnswerEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
export default {
name: "AnswerEditor",
Expand All @@ -34,7 +35,7 @@ export default {
this.error = "You can't submit an empty answer!";
return;
}
const endpoint = `/api/v1/answers/${this.uuid}/`;
const endpoint = `${endpoints["answersDetail"]}${this.uuid}/`;
try {
await axios.put(endpoint, { body: this.answerBody });
this.$router.push({
Expand All @@ -49,7 +50,7 @@ export default {
},
async beforeRouteEnter(to, from, next) {
// get the answer's data from the REST API and set two data properties for the component
const endpoint = `/api/v1/answers/${to.params.uuid}/`;
const endpoint = `${endpoints["answersDetail"]}${to.params.uuid}/`;
try {
const response = await axios.get(endpoint);
return next(
Expand Down
3 changes: 2 additions & 1 deletion src/vite-frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
export default {
name: "home-view",
Expand All @@ -40,7 +41,7 @@ export default {
methods: {
async getQuestions() {
// make a GET Request to the questions list endpoint and populate the questions array
let endpoint = "/api/v1/questions/";
let endpoint = endpoints["questionsCRUD"];
if (this.next) {
endpoint = this.next;
}
Expand Down
5 changes: 3 additions & 2 deletions src/vite-frontend/src/views/QuestionEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
export default {
name: "QuestionEditor",
Expand All @@ -29,7 +30,7 @@ export default {
methods: {
async performNetworkRequest() {
// Tell the REST API to create or update a Question instance;
let endpoint = "/api/v1/questions/";
let endpoint = endpoints["questionsCRUD"];
let method = "POST";
if (this.slug !== undefined && this.slug !== "") {
endpoint += `${this.slug}/`;
Expand Down Expand Up @@ -64,7 +65,7 @@ export default {
// if the component is used to update a question
// get the question's data from the REST API
if (to.params.slug !== undefined && to.params.slug !== "") {
const endpoint = `/api/v1/questions/${to.params.slug}/`;
const endpoint = `${endpoints["questionsCRUD"]}${to.params.slug}/`;
try {
const response = await axios.get(endpoint);
return next((vm) => (vm.questionBody = response.data.content));
Expand Down
9 changes: 5 additions & 4 deletions src/vite-frontend/src/views/QuestionView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

<script>
import { axios } from "@/common/api.service.js";
import { endpoints } from "@/common/endpoints.js";
import AnswerComponent from "@/components/Answer.vue";
import QuestionActions from "@/components/QuestionActions.vue";
Expand Down Expand Up @@ -94,7 +95,7 @@ export default {
},
async getQuestionData() {
// get the details of a question instance from the REST API and call setPageTitle
const endpoint = `/api/v1/questions/${this.slug}/`;
const endpoint = `${endpoints["questionsCRUD"]}${this.slug}/`;
try {
const response = await axios.get(endpoint);
this.question = response.data;
Expand All @@ -108,7 +109,7 @@ export default {
},
async getQuestionAnswers() {
// get a page of answers for a single question from the REST API's paginated 'Questions Endpoint'
let endpoint = `/api/v1/questions/${this.slug}/answers/`;
let endpoint = `${endpoints["questionsAnswersList"]}${this.slug}/`;
if (this.next) {
endpoint = this.next;
}
Expand All @@ -134,7 +135,7 @@ export default {
this.error = "You can't send an empty answer!";
return;
}
const endpoint = `/api/v1/questions/${this.slug}/answer/`;
const endpoint = `${endpoints["questionsNewAnswer"]}${this.slug}/`;
try {
const response = await axios.post(endpoint, {
body: this.newAnswerBody,
Expand All @@ -153,7 +154,7 @@ export default {
},
async deleteAnswer(answer) {
// delete a given answer from the answers array and make a delete request to the REST API
const endpoint = `/api/v1/answers/${answer.uuid}/`;
const endpoint = `${endpoints["answersDetail"]}${answer.uuid}/`;
try {
await axios.delete(endpoint);
this.answers.splice(this.answers.indexOf(answer), 1);
Expand Down

0 comments on commit fdfc6da

Please sign in to comment.