Skip to content

Commit

Permalink
Merge pull request #6 from rebekahgrandey/vet-home-page
Browse files Browse the repository at this point in the history
fixed register in UserController, fixed repos, changed homepage direc…
  • Loading branch information
rebekahgrandey authored Feb 8, 2023
2 parents 574f274 + 8388774 commit 608aeeb
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 22 deletions.
11 changes: 6 additions & 5 deletions PawPanion/Controllers/PetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace PawPanion.Controllers
{
/*[Authorize]*/
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class PetController : ControllerBase
Expand All @@ -22,6 +22,7 @@ public PetController(IPetRepository petRepository, IUserRepository userRepositor
_userRepository=userRepository;
}


[HttpGet("{id}")]
public IActionResult GetPetById(int id)
{
Expand All @@ -31,18 +32,18 @@ public IActionResult GetPetById(int id)
NotFound();
}
return Ok(post);
}
}

[HttpGet("{id}")]
/*[HttpGet("{id}")]
public IActionResult GetPetByOwnerId(int id)
{
var post = _petRepository.GetPetByOwnerId(id);
if (post != null)
{
NotFound();
}
return Ok(post);
}
return Ok(post);
} */

[HttpGet]
public ActionResult GetAll()
Expand Down
21 changes: 20 additions & 1 deletion PawPanion/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace PawPanion.Controllers
{
/*[Authorize]*/
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
Expand All @@ -25,11 +25,30 @@ public IActionResult GetUser(string firebaseUserId)
return Ok(_userRepository.GetByFirebaseUserId(firebaseUserId));
}

[HttpGet("DoesUserExist/{firebaseUserId}")]
public IActionResult DoesUserExist(string firebaseUserId)
{
var userProfile = _userRepository.GetByFirebaseUserId(firebaseUserId);
if (userProfile == null)
{
return NotFound();
}
return Ok();
}

[HttpGet]
public ActionResult Index()
{
var users = _userRepository.GetAllUsers();
return Ok(users);
}

[HttpPost]
public IActionResult Register(User user)
{
_userRepository.Add(user);
return CreatedAtAction(nameof(GetUser),
new { firebaseUserId = user.FirebaseUserId }, user);
}
}
}
1 change: 1 addition & 0 deletions PawPanion/Repositories/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IUserRepository
{
List <User> GetAllUsers();
User GetByFirebaseUserId(string firebaseUserId);
void Add(User user);
}
}
24 changes: 18 additions & 6 deletions PawPanion/Repositories/PetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,37 @@ public List<Pet> GetAllPets()
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"
SELECT p.Id, p.Name, Breed, IsMale, Birthdate, p.OwnerId, IsDog,
p.ImageLocation, u.Name, u.Id
FROM Pet p JOIN [User] u ON p.OwnerId = u.Id
SELECT p.Id AS PetId, p.Name AS PetName, Breed, IsMale, Birthdate, p.OwnerId, IsDog, p.ImageLocation, u.Name AS UserName, u.Id AS UserId, FirebaseUserId, Email, Phone, u.ImageLocation
FROM Pet p
JOIN [User] u ON p.OwnerId = u.Id
ORDER BY u.Name
";

using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Pet> pets = new List<Pet>();

while (reader.Read())
{
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")
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")
};


Expand Down
28 changes: 25 additions & 3 deletions PawPanion/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public User GetByFirebaseUserId(string firebaseUserId)
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"
SELECT Id, FirebaseUserId, Name, Email, Phone, ImageLocation, IsVet
FROM User
SELECT Id, FirebaseUserId, [Name], Email, Phone, ImageLocation, IsVet
FROM [User]
WHERE FirebaseUserId = @FirebaseuserId";

DbUtils.AddParameter(cmd, "@FirebaseUserId", firebaseUserId);
Expand Down Expand Up @@ -57,7 +57,7 @@ public List<User> GetAllUsers()
cmd.CommandText = @"
SELECT Id, FirebaseUserId, Name, Email, Phone, ImageLocation, IsVet
FROM [User]
ORDER BY Name;";
ORDER BY [Name];";

using (SqlDataReader reader = cmd.ExecuteReader())
{
Expand Down Expand Up @@ -86,5 +86,27 @@ FROM [User]
}
}

public void Add(User user)
{
using (var conn = Connection)
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"INSERT INTO User (FirebaseUserId, Name, Email, Phone, ImageLocation, IsVet)
OUTPUT INSERTED.ID
VALUES (@FirebaseUserId, @Name, @Email)";
DbUtils.AddParameter(cmd, "@FirebaseUserId", user.FirebaseUserId);
DbUtils.AddParameter(cmd, "@Name", user.Name);
DbUtils.AddParameter(cmd, "@Email", user.Email);
DbUtils.AddParameter(cmd, "Phone", user.Phone);
DbUtils.AddParameter(cmd, "ImageLocation", user.ImageLocation);
DbUtils.AddParameter(cmd, "IsVet", user.IsVet);

user.Id = (int)cmd.ExecuteScalar();
}
}
}

}
}
7 changes: 2 additions & 5 deletions PawPanion/client/src/components/ApplicationViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { Routes, Route, Navigate } from "react-router-dom";
import Login from "./Login";
import Register from "./Register";
import { HomePage } from "./userViews/HomePage";

export default function ApplicationViews({ isLoggedIn }) {
return (
Expand All @@ -11,11 +12,7 @@ export default function ApplicationViews({ isLoggedIn }) {

<Route
index
element={isLoggedIn ? <VetHomePage /> : <Navigate to="/login" />}
/>
<Route
path="UserList"
element={isLoggedIn ? <UserList /> : <Navigate to="/UserList" />}
element={isLoggedIn ? <HomePage /> : <Navigate to="/login" />}
/>
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
Expand Down
10 changes: 10 additions & 0 deletions PawPanion/client/src/components/userViews/HomePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { VetHomePage } from "./VetHomePage";

export const HomePage = () => {

return (
<>
<VetHomePage />
</>
)
}
33 changes: 31 additions & 2 deletions PawPanion/client/src/components/userViews/VetHomePage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
import React from "react";
import { Routes, Route, Navigate } from "react-router-dom";
import { getAllPets } from "../../modules/petManager";
import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { Card } from "reactstrap";

export const VetHomePage = () => {
const [pets, setPets] = useState([]);

const getAll = () => {
getAllPets().then(pets => setPets(pets));
}

useEffect(() => {
getAll();
}, []);

return (
<div className="pet-container">
<div className="pet_list_header"><div className="pet-left">Owner</div><div className="pet-center">Number</div><div className="pet-right">Pet</div></div>
<div className="row justify-content-center">
{pets.map((pet) => {
return <Card key={pet.id}><div className="pet-list-container">
<div className="pet_elements pet-left">{pet.owner.phone}</div>
<div className="post_elements post-center">{pet.owner.name}</div>
<div className="post_elements post-right"><Link to={``}>{pet.name}</Link></div>
<div></div>
</div></Card>
})}
</div>
</div>
)
}
70 changes: 70 additions & 0 deletions PawPanion/client/src/modules/petManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getToken } from "./authManager";

const baseUrl = '/api/pet';

export const getAllPets = () => {
return getToken().then((token) => {
return fetch(baseUrl, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
}).then((res) => {
if (res.ok) {
return res.json();
} else {
throw new Error(
"An unknown error occurred.",
);
}
});
});
};

export const addPet = (pet) => {
return getToken().then((token) => {
return fetch(baseUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify(pet)
});
});


};

export const getPetById = (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.",
);
}
});
});
};

export const editPet = (pet, id) => {
return getToken().then((token) => {
return fetch(`${baseUrl}/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify(pet)
});
});

};

0 comments on commit 608aeeb

Please sign in to comment.