Skip to content

Commit

Permalink
Dodane mogucnosti dodavanja, uredivanja i brisanja jela
Browse files Browse the repository at this point in the history
  • Loading branch information
Juric22 committed Jun 13, 2024
1 parent 7d01afb commit 72f0c43
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 74 deletions.
40 changes: 37 additions & 3 deletions Software/SZUUP/SZUUP/FrmPopisJela.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ private void ShowJela()
private void btnDodajJelo_Click(object sender, EventArgs e)
{
FrmUnos frmUnos = new FrmUnos();
frmUnos.ShowDialog();
if (frmUnos.ShowDialog() == DialogResult.OK)
{
ShowJela(); // Refresh the list of meals
}
}

private void btnPretraziJelo_Click(object sender, EventArgs e)
Expand All @@ -53,12 +56,43 @@ private void btnPretraziJelo_Click(object sender, EventArgs e)

private void btnUrediJelo_Click(object sender, EventArgs e)
{
// Code to handle edit functionality
if (dgvJela.SelectedRows.Count == 0)
{
MessageBox.Show("Odaberite jelo za uređivanje.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

int selectedRowIndex = dgvJela.SelectedRows[0].Index;
int selectedJeloId = (int)dgvJela.Rows[selectedRowIndex].Cells["Id_jelo"].Value;

Jelo selectedJelo = JeloRepozitorij.GetJelo(selectedJeloId);

using (FrmUnos frmUnos = new FrmUnos(selectedJelo))
{
if (frmUnos.ShowDialog() == DialogResult.OK)
{
ShowJela(); // Refresh the list after editing
}
}
}

private void btnObrisiJelo_Click(object sender, EventArgs e)
{
// Code to handle delete functionality
if (dgvJela.SelectedRows.Count == 0)
{
MessageBox.Show("Odaberite jelo za brisanje.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

int selectedRowIndex = dgvJela.SelectedRows[0].Index;
int selectedJeloId = (int)dgvJela.Rows[selectedRowIndex].Cells["Id_jelo"].Value;

var result = MessageBox.Show("Jeste li sigurni da želite obrisati odabrano jelo?", "Potvrda", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
JeloRepozitorij.ObrisiJelo(selectedJeloId);
ShowJela(); // Refresh the list after deletion
}
}

private void dgvPopisJela_CellContentClick(object sender, DataGridViewCellEventArgs e)
Expand Down
16 changes: 8 additions & 8 deletions Software/SZUUP/SZUUP/FrmUnos.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 100 additions & 21 deletions Software/SZUUP/SZUUP/FrmUnos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ namespace SZUUP
{
public partial class FrmUnos : Form
{
private Jelo _jeloToEdit;

public FrmUnos()
{
InitializeComponent();
this.Load += new EventHandler(FrmUnos_Load);
this.cmbKategorija.SelectedIndexChanged += new EventHandler(cmbKategorija_SelectedIndexChanged);
this.txtDetalji.TextChanged += new EventHandler(txtDetalji_TextChanged);
this.label3.Click += new EventHandler(label3_Click);
}

public FrmUnos(Jelo jelo) : this()
{
_jeloToEdit = jelo;
PopulateFormFields(jelo);
}

private void FrmUnos_Load(object sender, EventArgs e)
Expand All @@ -27,60 +38,128 @@ private void LoadCategories()
if (kategorije != null && kategorije.Count > 0)
{
cmbKategorija.DataSource = kategorije;
cmbKategorija.DisplayMember = "NazivKategorije"; // Display the renamed category name
cmbKategorija.ValueMember = "Id_Kategorija"; // Use the category ID as the value
cmbKategorija.DisplayMember = "NazivKategorije";
cmbKategorija.ValueMember = "Id_Kategorija";

if (_jeloToEdit != null)
{
cmbKategorija.SelectedValue = _jeloToEdit.Id_Kategorija;
}
}
else
{
MessageBox.Show("No categories found.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
// Removed the MessageBox for no data scenario
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnSpremi_Click(object sender, EventArgs e)
private void PopulateFormFields(Jelo jelo)
{
// Show a confirmation dialog
var result = MessageBox.Show("Jeste li sigurni da želite spremiti podatke?", "Potvrda",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
txtNaziv.Text = jelo.Naziv;
txtCijena.Text = jelo.Cijena.ToString();
txtDetalji.Text = jelo.Detalji;
}

if (result == DialogResult.Yes)
private void btnSpremi_Click(object sender, EventArgs e)
{
try
{
// Validacija unosa
if (!ValidateInput())
{
return;
}

var selectedCategory = cmbKategorija.SelectedItem as Kategorija;
if (selectedCategory == null)
{
MessageBox.Show("Odaberite ispravnu kategoriju jela.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

if (_jeloToEdit == null)
{
_jeloToEdit = new Jelo();
}

_jeloToEdit.Id_Kategorija = selectedCategory.Id_Kategorija;
_jeloToEdit.Naziv = txtNaziv.Text;
_jeloToEdit.Cijena = float.Parse(txtCijena.Text);
_jeloToEdit.Detalji = txtDetalji.Text;

if (_jeloToEdit.Id_jelo == 0)
{
JeloRepozitorij.DodajJelo(_jeloToEdit);
}
else
{
JeloRepozitorij.UpdateJelo(_jeloToEdit); // Assuming you have an UpdateJelo method in your repository
}

MessageBox.Show("Podaci su uspješno spremljeni.", "Uspjeh", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.DialogResult = DialogResult.OK; // Set dialog result to OK to indicate successful save
this.Close(); // Close the form after saving
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private bool ValidateInput()
{
if (cmbKategorija.SelectedItem == null)
{
MessageBox.Show("Odaberite kategoriju jela.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}

if (string.IsNullOrWhiteSpace(txtNaziv.Text))
{
MessageBox.Show("Unesite naziv jela.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}

if (!float.TryParse(txtCijena.Text, out float cijena) || cijena <= 0)
{
MessageBox.Show("Unesite ispravnu cijenu jela.", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}

// Ostatku validacije možete dodati prema potrebi (npr. za detalje jela)

return true;
}

private void btnOdustani_Click(object sender, EventArgs e)
{
var result = MessageBox.Show("Odustajete od unosa jela?", "Potvrda",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
var result = MessageBox.Show("Odustajete od unosa jela?", "Potvrda", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
FrmPrijava frmPrijava = new FrmPrijava();
frmPrijava.Show();
this.Close();
}
}

private void btnUredi_Click(object sender, EventArgs e)
private void cmbKategorija_SelectedIndexChanged(object sender, EventArgs e)
{
// Edit button click event handler
// Add code to handle selected index change if needed
}

private void cmbKategorija_SelectedIndexChanged(object sender, EventArgs e)
private void txtDetalji_TextChanged(object sender, EventArgs e)
{
// Handle the combo box selection change if needed
// Add code to handle text changed if needed
}

private void label3_Click(object sender, EventArgs e)
{
// Handle label click event if needed
// Add code to handle label click if needed
}

private void txtDetalji_TextChanged(object sender, EventArgs e)
{
// Handle text change event if needed
}
// Ostale event handler metode možete dodati prema potrebi
}
}
11 changes: 5 additions & 6 deletions Software/SZUUP/SZUUP/Models/Kategorija.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SZUUP.Models
{
Expand All @@ -11,7 +7,10 @@ public class Kategorija
public int Id_Kategorija { get; set; }
public string NazivKategorije { get; set; }
public string Opis { get; set; }

public override string ToString()
{
return NazivKategorije;
}
}
}


57 changes: 53 additions & 4 deletions Software/SZUUP/SZUUP/Repozitoris/JeloRepozitorij.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SZUUP.Models;

namespace SZUUP.Repozitoris
Expand Down Expand Up @@ -43,10 +40,57 @@ public static List<Jelo> GetJela()
return jela;
}

public static void DodajJelo(Jelo jelo)
{
int newIdJelo = GetNextIdJelo();
string sql = $"INSERT INTO jelo (Id_jelo, Kategorija, Naziv, Cijena, Detalji_o_jelu) VALUES ({newIdJelo}, {jelo.Id_Kategorija}, '{SanitizeInput(jelo.Naziv)}', {jelo.Cijena}, '{SanitizeInput(jelo.Detalji)}')";

DB.OpenConnection();
DB.ExecuteCommand(sql);
DB.CloseConnection();
}

public static void UpdateJelo(Jelo jelo)
{
string sql = $"UPDATE jelo SET Kategorija = {jelo.Id_Kategorija}, Naziv = '{SanitizeInput(jelo.Naziv)}', Cijena = {jelo.Cijena}, Detalji_o_jelu = '{SanitizeInput(jelo.Detalji)}' WHERE Id_jelo = {jelo.Id_jelo}";

DB.OpenConnection();
DB.ExecuteCommand(sql);
DB.CloseConnection();
}

public static void ObrisiJelo(int id_jelo)
{
string sql = $"DELETE FROM jelo WHERE Id_jelo = {id_jelo}";

DB.OpenConnection();
DB.ExecuteCommand(sql);
DB.CloseConnection();
}

private static int GetNextIdJelo()
{
int nextId = 1; // Default value if table is empty
string sql = "SELECT MAX(Id_jelo) AS MaxId FROM jelo";
DB.OpenConnection();
var reader = DB.GetDataReader(sql);
if (reader.HasRows)
{
reader.Read();
if (reader["MaxId"] != DBNull.Value)
{
nextId = Convert.ToInt32(reader["MaxId"]) + 1;
}
reader.Close();
}
DB.CloseConnection();
return nextId;
}

private static Jelo CreateObject(SqlDataReader reader)
{
int id_jelo = int.Parse(reader["Id_jelo"].ToString());
int id_kategorija = int.Parse(reader["Id_Kategorija"].ToString());
int id_kategorija = int.Parse(reader["Kategorija"].ToString()); // Corrected column name
string naziv = reader["Naziv"].ToString();
float cijena = float.Parse(reader["Cijena"].ToString());
string detalji = reader["Detalji_o_jelu"].ToString();
Expand All @@ -62,5 +106,10 @@ private static Jelo CreateObject(SqlDataReader reader)

return jelo;
}

private static string SanitizeInput(string input)
{
return input.Replace("'", "''"); // Simple sanitization for SQL injection prevention
}
}
}
Loading

0 comments on commit 72f0c43

Please sign in to comment.