From 89511ebcd16f37d7fa7c592e605373e21b643e63 Mon Sep 17 00:00:00 2001 From: Rebekah Taylor Date: Wed, 8 Feb 2023 19:20:55 -0800 Subject: [PATCH] updated img urls in seed data | view current user pets functionality | started user homepage views --- PawPanion/Controllers/PetController.cs | 11 ++-- PawPanion/Controllers/UserController.cs | 7 ++ PawPanion/Repositories/IPetRepository.cs | 2 +- PawPanion/Repositories/PetRepository.cs | 65 +++++++++++++++++-- .../src/components/userViews/HomePage.js | 19 ++++-- .../src/components/userViews/OwnerHomePage.js | 32 +++++++++ PawPanion/client/src/modules/petManager.js | 19 ++++++ PawPanion/client/src/modules/userManager.js | 22 +++++++ SQL/02_Seed_Data.sql | 4 +- 9 files changed, 164 insertions(+), 17 deletions(-) create mode 100644 PawPanion/client/src/modules/userManager.js diff --git a/PawPanion/Controllers/PetController.cs b/PawPanion/Controllers/PetController.cs index db554b3..f0db04d 100644 --- a/PawPanion/Controllers/PetController.cs +++ b/PawPanion/Controllers/PetController.cs @@ -32,18 +32,19 @@ public IActionResult GetPetById(int id) NotFound(); } return Ok(post); - } + } - /*[HttpGet("{id}")] - public IActionResult GetPetByOwnerId(int id) + [HttpGet("UserPets")] + public IActionResult GetPetsByOwner(int id) { - var post = _petRepository.GetPetByOwnerId(id); + var currentUser = GetCurrentUser(); + var post = _petRepository.GetUserPets(currentUser.Id); //.GetUserPets from PetRepository if (post != null) { NotFound(); } return Ok(post); - } */ + } [HttpGet] public ActionResult GetAll() diff --git a/PawPanion/Controllers/UserController.cs b/PawPanion/Controllers/UserController.cs index 212ed8f..d673842 100644 --- a/PawPanion/Controllers/UserController.cs +++ b/PawPanion/Controllers/UserController.cs @@ -4,6 +4,7 @@ using System.Security.Permissions; using PawPanion.Models; using PawPanion.Repositories; +using System.Security.Claims; namespace PawPanion.Controllers { @@ -50,5 +51,11 @@ public IActionResult Register(User user) return CreatedAtAction(nameof(GetUser), new { firebaseUserId = user.FirebaseUserId }, user); } + + /* private User GetCurrentUser() + { + var firebaseUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; + return _userRepository.GetByFirebaseUserId(firebaseUserId); + } */ } } diff --git a/PawPanion/Repositories/IPetRepository.cs b/PawPanion/Repositories/IPetRepository.cs index cd9b6b8..c9be9ad 100644 --- a/PawPanion/Repositories/IPetRepository.cs +++ b/PawPanion/Repositories/IPetRepository.cs @@ -8,8 +8,8 @@ public interface IPetRepository void Add(Pet pet); void Edit(Pet pet); List GetAllPets(); + List GetUserPets(int userId); Pet GetPetById(int id); - Pet GetPetByOwnerId(int id); void Delete(int id); } } \ No newline at end of file diff --git a/PawPanion/Repositories/PetRepository.cs b/PawPanion/Repositories/PetRepository.cs index ca61e1a..3c158d5 100644 --- a/PawPanion/Repositories/PetRepository.cs +++ b/PawPanion/Repositories/PetRepository.cs @@ -102,7 +102,7 @@ public Pet GetPetById(int id) } } - public Pet GetPetByOwnerId(int id) + /*public Pet GetPetByOwnerId(int id) { using (var conn = Connection) { @@ -110,8 +110,7 @@ public Pet GetPetByOwnerId(int id) using (var cmd = conn.CreateCommand()) { cmd.CommandText = @" - SELECT p.Id, p.Name, Breed, IsMale, Birthdate, p.OwnerId, IsDog, - p.ImageLocation, u.Name, u.Id + SELECT p.Id AS PetId, p.Name AS PetName, Breed, IsMale, Birthdate, p.OwnerId, IsDog, p.ImageLocation, u.Name, u.Id FROM Pet p JOIN [User] u ON p.OwnerId = u.Id WHERE p.OwnerId = @Id"; @@ -123,12 +122,13 @@ public Pet GetPetByOwnerId(int id) { Pet pet = new Pet { - Id = DbUtils.GetInt(reader, "Id"), - Name = DbUtils.GetString(reader, "Name"), + Id = DbUtils.GetInt(reader, "PetId"), + Name = DbUtils.GetString(reader, "PetName"), Breed = DbUtils.GetString(reader, "Breed"), IsMale = reader.GetBoolean(reader.GetOrdinal("IsMale")), Birthdate = DbUtils.GetDateTime(reader, "Birthdate"), OwnerId = DbUtils.GetInt(reader, "OwnerId") + }; return pet; } @@ -137,6 +137,61 @@ public Pet GetPetByOwnerId(int id) } } } + */ + + public List GetUserPets(int userId) + { + using (var conn = Connection) + { + conn.Open(); + using (var cmd = conn.CreateCommand()) + { + cmd.CommandText = @" + SELECT p.Id AS PetId, p.Name AS PetName, Breed, IsMale, Birthdate, p.OwnerId, isDog, p.ImageLocation, + u.Id AS UserId, FirebaseUserId, u.Name AS UserName, Email, Phone, u.ImageLocation, IsVet + FROM Pet p + JOIN [User] u ON u.Id = @userId + WHERE @userId = p.OwnerId + ORDER BY p.Name"; + cmd.Parameters.AddWithValue("@userId", userId); + + using (SqlDataReader reader = cmd.ExecuteReader()) + { + var pets = new List(); + + while (reader.Read()) + { + pets.Add(NewPetFromReader(reader)); + } + return pets; + } + } + } + } + + private Pet NewPetFromReader(SqlDataReader reader) + { + return new Pet() + { + Id = DbUtils.GetInt(reader, "PetId"), + Name = DbUtils.GetString(reader, "PetName"), + Breed = DbUtils.GetString(reader, "Breed"), + IsMale = reader.GetBoolean(reader.GetOrdinal("IsMale")), + Birthdate = DbUtils.GetDateTime(reader, "Birthdate"), + OwnerId = DbUtils.GetInt(reader, "OwnerId"), + Owner = new User() + { + Id = DbUtils.GetInt(reader, "UserId"), + FirebaseUserId = DbUtils.GetString(reader, "FirebaseUserId"), + Name = DbUtils.GetString(reader, "UserName"), + Email = DbUtils.GetString(reader, "Email"), + Phone = DbUtils.GetString(reader, "Phone"), + ImageLocation = DbUtils.GetString(reader, "ImageLocation") + }, + IsDog = reader.GetBoolean(reader.GetOrdinal("IsDog")), + ImageLocation = DbUtils.GetString(reader, "ImageLocation") + }; + } public void Add(Pet pet) { diff --git a/PawPanion/client/src/components/userViews/HomePage.js b/PawPanion/client/src/components/userViews/HomePage.js index cd5798a..25a2d63 100644 --- a/PawPanion/client/src/components/userViews/HomePage.js +++ b/PawPanion/client/src/components/userViews/HomePage.js @@ -1,10 +1,21 @@ +import firebase from "firebase/app"; +import "firebase/auth"; import { VetHomePage } from "./VetHomePage"; +import { OwnerHomePage } from "./OwnerHomePage"; export const HomePage = () => { + var homePage; + + if (!firebase.auth().currentUser.isVet) { + homePage = ; + } else { + homePage = ; + } + return ( - <> - - - ) + + ); } \ No newline at end of file diff --git a/PawPanion/client/src/components/userViews/OwnerHomePage.js b/PawPanion/client/src/components/userViews/OwnerHomePage.js index e69de29..1851c8f 100644 --- a/PawPanion/client/src/components/userViews/OwnerHomePage.js +++ b/PawPanion/client/src/components/userViews/OwnerHomePage.js @@ -0,0 +1,32 @@ +import "firebase/auth"; +import { getUserPets } from "../../modules/petManager"; +import React, { useEffect, useState } from "react"; +import { Link } from "react-router-dom"; +import { Card } from "reactstrap"; + +export const OwnerHomePage = () => { + const [ownerPets, setOwnerPets] = useState([]); + + const getAll = () => { + getUserPets().then(ownerPets => setOwnerPets(ownerPets)); + } + + useEffect(() => { + getAll(); + }, []); + + return ( +
+ {ownerPets.map((pet) => { + return
+
+
{pet.name}
+
{pet.birthdate}
+
{pet.breed}
+
EditDelete
+
View All Records
+
+ })} +
+ ) +} \ No newline at end of file diff --git a/PawPanion/client/src/modules/petManager.js b/PawPanion/client/src/modules/petManager.js index ab3b5cc..b1ce3c9 100644 --- a/PawPanion/client/src/modules/petManager.js +++ b/PawPanion/client/src/modules/petManager.js @@ -21,6 +21,25 @@ export const getAllPets = () => { }); }; +export const getUserPets = (id) => { + return getToken().then((token) => { + return fetch(`${baseUrl}/UserPets`, { + method: "GET", + headers: { + Authorization: `Bearer ${token}` + }, + }).then((res) => { + if (res.ok) { + return res.json(); + } else { + throw new Error( + "An unknown error occurred while trying to get pictures.", + ); + } + }); + }); +}; + export const addPet = (pet) => { return getToken().then((token) => { return fetch(baseUrl, { diff --git a/PawPanion/client/src/modules/userManager.js b/PawPanion/client/src/modules/userManager.js new file mode 100644 index 0000000..37da90f --- /dev/null +++ b/PawPanion/client/src/modules/userManager.js @@ -0,0 +1,22 @@ +import { getToken } from "./authManager"; + +const baseUrl = "/api/user"; + +export const getCurrentUserByFirebaseId = (id) => { + return getToken().then((token) => { + return fetch(`${baseUrl}/${id}`, { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + }, + }).then((res) => { + if (res.ok) { + return res.json(); + } else { + throw new Error( + "An unknown error occured.", + ); + } + }); + }); +}; \ No newline at end of file diff --git a/SQL/02_Seed_Data.sql b/SQL/02_Seed_Data.sql index cfd4834..74bc4d0 100644 --- a/SQL/02_Seed_Data.sql +++ b/SQL/02_Seed_Data.sql @@ -13,8 +13,8 @@ insert into [User] ([Id], FirebaseUserId, [Name], Email, Phone, ImageLocation, i set identity_insert [User] off set identity_insert [Pet] on -insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (1, 'Annie', 'Ragdoll/mix', '0', '2016-05-05', 3, '0', 'https://pasteboard.co/RTqrisoyChZm.png'); -insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (2, 'Lottie', 'Britney/Cocker Spaniel', '0', '2022-11-30', 3, '1', 'https://pasteboard.co/6AOGSiqa4fXP.png'); +insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (1, 'Annie', 'Ragdoll/mix', '0', '2016-05-05', 3, '0', 'https://www.dropbox.com/s/kkroxqyd1mynfhs/Annie.jpg?raw=1'); +insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (2, 'Lottie', 'Britney/Cocker Spaniel', '0', '2022-11-30', 3, '1', 'https://www.dropbox.com/s/ltnvs4w3nl44efd/Lottie.jpg?raw=1'); insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (3, 'Banjo', 'Mountain Cur', '1', '2020-09-01', 4, '1', 'https://www.dogbreedslist.info/uploads/dog-pictures/mountain-cur-2.jpg'); insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (4, 'Luna', 'mixed breed', '0', '2015-06-16', 7, '1', null); insert into [Pet] ([Id], [Name], Breed, IsMale, Birthdate, OwnerId, IsDog, ImageLocation) values (5, 'Baby Cat', 'Tortie mix', '0', '2019-11-10', 5, '0', null);