Skip to content

Commit

Permalink
updated img urls in seed data | view current user pets functionality …
Browse files Browse the repository at this point in the history
…| started user homepage views
  • Loading branch information
rebekahgrandey committed Feb 9, 2023
1 parent 608aeeb commit 89511eb
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 17 deletions.
11 changes: 6 additions & 5 deletions PawPanion/Controllers/PetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions PawPanion/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Security.Permissions;
using PawPanion.Models;
using PawPanion.Repositories;
using System.Security.Claims;

namespace PawPanion.Controllers
{
Expand Down Expand Up @@ -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);
} */
}
}
2 changes: 1 addition & 1 deletion PawPanion/Repositories/IPetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public interface IPetRepository
void Add(Pet pet);
void Edit(Pet pet);
List<Pet> GetAllPets();
List<Pet> GetUserPets(int userId);
Pet GetPetById(int id);
Pet GetPetByOwnerId(int id);
void Delete(int id);
}
}
65 changes: 60 additions & 5 deletions PawPanion/Repositories/PetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,15 @@ public Pet GetPetById(int id)
}
}

public Pet GetPetByOwnerId(int id)
/*public Pet GetPetByOwnerId(int id)
{
using (var conn = Connection)
{
conn.Open();
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";
Expand All @@ -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;
}
Expand All @@ -137,6 +137,61 @@ public Pet GetPetByOwnerId(int id)
}
}
}
*/

public List<Pet> 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<Pet>();

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)
{
Expand Down
19 changes: 15 additions & 4 deletions PawPanion/client/src/components/userViews/HomePage.js
Original file line number Diff line number Diff line change
@@ -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 = <OwnerHomePage />;
} else {
homePage = <VetHomePage />;
}

return (
<>
<VetHomePage />
</>
)
<nav>
{homePage}
</nav>
);
}
32 changes: 32 additions & 0 deletions PawPanion/client/src/components/userViews/OwnerHomePage.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="pet-container">
{ownerPets.map((pet) => {
return <div className="pet-list-container" key={pet.id}>
<div><img style={{ width: 200 }} src={pet.imageLocation} /></div>
<div>{pet.name}</div>
<div>{pet.birthdate}</div>
<div>{pet.breed}</div>
<div><Link to={``}>Edit</Link><Link to={``}>Delete</Link></div>
<div><Link to={``}>View All Records</Link></div>
</div>
})}
</div>
)
}
19 changes: 19 additions & 0 deletions PawPanion/client/src/modules/petManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
22 changes: 22 additions & 0 deletions PawPanion/client/src/modules/userManager.js
Original file line number Diff line number Diff line change
@@ -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.",
);
}
});
});
};
4 changes: 2 additions & 2 deletions SQL/02_Seed_Data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 89511eb

Please sign in to comment.