Skip to content

Commit

Permalink
Modifications for Lab Exercise 3
Browse files Browse the repository at this point in the history
  • Loading branch information
JimCarlTay committed Mar 9, 2023
1 parent 51e62d3 commit b1bad43
Show file tree
Hide file tree
Showing 106 changed files with 3,487 additions and 8 deletions.
Binary file modified .vs/ProjectEvaluation/taylorle2.metadata.v6.1
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/taylorle2.projects.v6.1
Binary file not shown.
Binary file modified .vs/TaylorLE2/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,026 changes: 1,026 additions & 0 deletions .vs/TaylorLE2/config/applicationhost.config

Large diffs are not rendered by default.

Binary file modified .vs/TaylorLE2/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/TaylorLE2/v17/.suo
Binary file not shown.
13 changes: 13 additions & 0 deletions BlogDataLibrary/Data/ISqlData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using BlogDataLibrary.Models;

namespace BlogDataLibrary.Data
{
public interface ISqlData
{
void AddPost(PostModel post);
UserModel Authenticate(string username, string password);
List<ListPostModel> ListPosts();
void Register(string username, string firstName, string lastName, string password);
ListPostModel ShowPostDetails(int id);
}
}
41 changes: 39 additions & 2 deletions BlogDataLibrary/Data/SqlData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlogDataLibrary.Database;
using BlogDataLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,14 +8,50 @@

namespace BlogDataLibrary.Data
{
public class SqlData
public class SqlData : ISqlData
{
private ISqlDataAccess _db;
private const string connectionStringName = "sqlDb";
private const string connectionStringName = "SqlDb";

public SqlData(ISqlDataAccess db)
{
_db = db;
}

public UserModel Authenticate(string username, string password)
{
UserModel result = _db.LoadData<UserModel, dynamic>("dbo.spUsers_Authenticate",
new { username, password },
connectionStringName,
true).FirstOrDefault();
return result;
}

public void Register(string username, string firstName, string lastName, string password)
{
_db.SaveData<dynamic>(
"dbo.spUsers_Register",
new { username, firstName, lastName, password },
connectionStringName,
true);
}

public void AddPost(PostModel post)
{
_db.SaveData("spPosts_Insert", new { post.UserId, post.Title, post.Body, post.DateCreated },
connectionStringName, true);
}

public List<ListPostModel> ListPosts()
{
return _db.LoadData<ListPostModel, dynamic>("dbo.spPosts_List", new { },
connectionStringName, true).ToList();
}

public ListPostModel ShowPostDetails(int id)
{
return _db.LoadData<ListPostModel, dynamic>("dbo.spPosts_Details", new { id },
connectionStringName, true).FirstOrDefault();
}
}
}
Binary file modified BlogDataLibrary/bin/Debug/net6.0/BlogDataLibrary.dll
Binary file not shown.
Binary file modified BlogDataLibrary/bin/Debug/net6.0/BlogDataLibrary.pdb
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7bf2bb5f4c5008b7b10de4f0c1c5eb4823da8b23
379d4d320222f608ba031de30d83c3f39d57ec42
Binary file modified BlogDataLibrary/obj/Debug/net6.0/BlogDataLibrary.dll
Binary file not shown.
Binary file modified BlogDataLibrary/obj/Debug/net6.0/BlogDataLibrary.pdb
Binary file not shown.
Binary file modified BlogDataLibrary/obj/Debug/net6.0/ref/BlogDataLibrary.dll
Binary file not shown.
Binary file modified BlogDataLibrary/obj/Debug/net6.0/refint/BlogDataLibrary.dll
Binary file not shown.
10 changes: 10 additions & 0 deletions BlogTestUI/BlogTestUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlogDataLibrary\BlogDataLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
128 changes: 126 additions & 2 deletions BlogTestUI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,133 @@
namespace BlogTestUI;
using BlogDataLibrary.Data;
using BlogDataLibrary.Database;
using BlogDataLibrary.Models;
using Microsoft.Extensions.Configuration;

namespace BlogTestUI;

internal class Program
{
static SqlData GetConnection()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");

IConfiguration config = builder.Build();
ISqlDataAccess dbAccess = new SqlDataAccess(config);
SqlData db = new SqlData(dbAccess);

return db;
}

static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
SqlData db = GetConnection();

Authenticate(db);

Register(db);

AddPost(db);

ListPosts(db);

ShowPostDetails(db);

Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}

private static UserModel GetCurrentUser(SqlData db)
{
Console.Write("Username: ");
string username = Console.ReadLine();

Console.Write("Password: ");
string password = Console.ReadLine();

UserModel user = db.Authenticate(username, password);

return user;
}

public static void Authenticate(SqlData db)
{
UserModel user = GetCurrentUser(db);

if (user == null)
{
Console.WriteLine("Invalid credentials.");
}
else
{
Console.WriteLine($"Welcome, {user.UserName}");
}
}

public static void Register(SqlData db)
{
Console.Write("Enter new username: ");
var username = Console.ReadLine();

Console.Write("Enter new password: ");
var password = Console.ReadLine();

Console.Write("Enter first name: ");
var firstName = Console.ReadLine();

Console.Write("Enter last name: ");
var lastName = Console.ReadLine();

db.Register(username, firstName, lastName, password);
}

private static void AddPost(SqlData db)
{
UserModel user = GetCurrentUser(db);

Console.Write("Title: ");
string title = Console.ReadLine();

Console.WriteLine("Write body: ");
string body = Console.ReadLine();

PostModel post = new PostModel
{
Title = title,
Body = body,
DateCreated = DateTime.Now,
UserId = user.Id
};

db.AddPost(post);
}

private static void ListPosts(SqlData db)
{
List<ListPostModel> posts = db.ListPosts();

foreach (ListPostModel post in posts)
{
Console.WriteLine($"{post.Id}. Title: {post.Title} by {post.UserName} [{post.DateCreated.ToString("yyyy-MM-dd")}]");
Console.WriteLine($"{post.Body.Substring(0, 20)}...");
Console.WriteLine();
}
}

private static void ShowPostDetails(SqlData db)
{
Console.Write("Enter a post ID: ");
int id = Int32.Parse(Console.ReadLine());

ListPostModel post = db.ShowPostDetails(id);
Console.WriteLine(post.Title);
Console.WriteLine($"by {post.FirstName} {post.LastName} [{post.UserName}]");

Console.WriteLine();

Console.WriteLine(post.Body);

Console.WriteLine(post.DateCreated.ToString("MMM d yyyy"));
}
}
13 changes: 13 additions & 0 deletions BlogTestUI/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"exclude": [
"**/bin",
"**/bower_components",
"**/jspm_packages",
"**/node_modules",
"**/obj",
"**/platforms"
],
"ConnectionStrings": {
"SqlDb": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BlogDB;Integrated Security=True;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;MultiSubnetFailover=False"
}
}
Binary file not shown.
Binary file added BlogTestUI/bin/Debug/net6.0/BlogDataLibrary.pdb
Binary file not shown.
Loading

0 comments on commit b1bad43

Please sign in to comment.