Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/
obj/
.vs
.vs
ExpensesWebApp.csproj.user
21 changes: 16 additions & 5 deletions Controllers/ExpenseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ExpensesWebApp.Utilities;

namespace ExpensesWebApp.Controllers
{
[Route("api/[controller]")]
[ApiController]

public class ExpenseController : ControllerBase

{
private readonly ILogger<ExpenseController> _logger;

public ExpenseController (ILogger<ExpenseController> logger)
public IExpenseManager _expenseManager;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Šituos darom private, nėra prasmės public palikt.


public ExpenseController(ILogger<ExpenseController> logger, IExpenseManager expenseManager)
{
_logger = logger;
_expenseManager = expenseManager;
}

[HttpGet]
public IEnumerable<Expense> Get()
public IEnumerable<ExpenseItem> Get()
{
return Enumerable.Range(1, 1).Select(index => new Expense
return Enumerable.Range(1, 1).Select(index => new ExpenseItem
{
ExpenseSum = 10
Expenses = 10
})
.ToArray();
}
[HttpPost]
public void Post (ExpenseItem expenseItem)
{
_expenseManager.Insert(expenseItem);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dar pridėk patikrinimus, ar atsiųsti laukai validūs. Tarkim Expenses kad būtų > 0. Expense_type_ID ir User_ID gal vėliau padarysim kad pačioj duombazės patikrintų (Uždėsim duombazės foreign keys).

}

}
}
15 changes: 15 additions & 0 deletions Expense.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ExpensesWebApp
{
public class ExpenseItem
{
public DateTime ExpenseDate { get; set; }
public double Expenses { get; set; }
public int Expense_type_ID { get; set; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vengiam visur underscore varduose. Pakeisk į ExpenseTypeId ir UserID. Aišku kai kas gal ir taip rašo, bet dažniausiai tai underscore nenaudoja.

public int User_ID { get; set; }
}
}
7 changes: 4 additions & 3 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using ExpensesWebApp.Utilities;

namespace ExpensesWebApp
{
Expand All @@ -16,13 +17,14 @@ public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton(typeof(IConnectionToDB), typeof(ConnectionToDB));
services.AddTransient(typeof(IExpenseManager), typeof(ExpenseManager));

}

Expand Down Expand Up @@ -53,9 +55,8 @@ public void InitializeDatabase()
var assembly = Assembly.GetExecutingAssembly();
var allResourceNames = assembly.GetManifestResourceNames();
var resourceName = allResourceNames[0];
string connStr = Configuration.GetConnectionString("DatabaseConnectionString");

using var conn = new SqlConnection(connStr);
using var conn = new SqlConnection(Configuration.GetConnectionString("DatabaseConnectionString"));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Čia panaudok tą patį ConnectionToDB iš dependency injection

using Stream stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader reader = new StreamReader(stream);
conn.Open();
Expand Down
22 changes: 22 additions & 0 deletions Utilities/ConnectionToDB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;


namespace ExpensesWebApp.Utilities

{
public class ConnectionToDB : IConnectionToDB
{
public IConfiguration Configuration { get; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private šitie visur

public ConnectionToDB(IConfiguration configuration)
{
Configuration = configuration;
}
public SqlConnection Connect()
{
var conn = new SqlConnection(Configuration.GetConnectionString("DatabaseConnectionString"));
conn.Open();
return conn;
}
}
}
25 changes: 25 additions & 0 deletions Utilities/ExpenseManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Data.SqlClient;

namespace ExpensesWebApp.Utilities
{
public class ExpenseManager : IExpenseManager
{
public IConnectionToDB _connection;
public ExpenseManager(IConnectionToDB connection)
{
_connection = connection;
}
public void Insert(ExpenseItem expenseItem)
{
string query = "USE Expenses ";
query += "INSERT INTO dbo.expenses (date, expenses, expense_type_ID, user_ID) ";
query += "VALUES (@date, @expenses, @expense_type_ID, @user_ID)";
var insert = new SqlCommand(query, _connection.Connect());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

čia _connection.Connect() sukuria objektą kurį reikia dispose'int.

insert.Parameters.AddWithValue("@date", expenseItem.ExpenseDate);
insert.Parameters.AddWithValue("@expenses", expenseItem.Expenses);
insert.Parameters.AddWithValue("@expense_type_ID", expenseItem.Expense_type_ID);
insert.Parameters.AddWithValue("@user_ID", expenseItem.User_ID);
insert.ExecuteNonQuery();
}
}
}
12 changes: 12 additions & 0 deletions Utilities/IConnectionToDB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;

namespace ExpensesWebApp.Utilities
{
public interface IConnectionToDB
{
IConfiguration Configuration { get; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Čia nereikia configuration pridėt. Kam reikės tas pasiims configuration iš dependency injection. Palik tik Connect() interfeise.


SqlConnection Connect();
}
}
7 changes: 7 additions & 0 deletions Utilities/IExpenseManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ExpensesWebApp.Utilities
{
public interface IExpenseManager
{
void Insert(ExpenseItem expenseItem);
}
}
2 changes: 1 addition & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
Expand Down
7 changes: 5 additions & 2 deletions expense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

namespace ExpensesWebApp
{
public class Expense
public class ExpenseItem
{
public double ExpenseSum { get; set; }
public DateTime ExpenseDate { get; set; }
public double Expenses { get; set; }
public int Expense_type_ID { get; set; }
public int User_ID { get; set; }
}
}