DemoService/AddEditWindow.axaml.cs

112 lines
4.0 KiB
C#
Raw Permalink Normal View History

2024-09-05 13:11:39 +00:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
2024-09-09 13:26:07 +00:00
using Avalonia.Media.Imaging;
using DemoService.Context;
using DemoService.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Net;
2024-09-05 13:11:39 +00:00
namespace DemoService;
public partial class AddEditWindow : Window
{
2024-09-09 13:26:07 +00:00
private bool AdminMode;
2024-09-10 13:12:58 +00:00
private int serviceID;
2024-09-05 13:11:39 +00:00
public AddEditWindow()
{
InitializeComponent();
2024-09-09 13:26:07 +00:00
Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
}
public AddEditWindow(bool AdminMode)
{
InitializeComponent();
this.AdminMode = AdminMode;
2024-09-10 13:12:58 +00:00
ServiceIDTextBlock.IsVisible = false;
EditServiceButton.IsVisible = false;
Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
}
public AddEditWindow(bool AdminMode, int ID)
{
InitializeComponent();
this.AdminMode = AdminMode;
serviceID = ID;
ServiceID.Text = serviceID.ToString();
using (var context = new OvsyannikovContext())
{
var service = context.Services.Find(serviceID);
ServiceName.Text = service.Title;
Cost.Text = Convert.ToString(service.Cost);
DurationInSeconds.Text = Convert.ToString(service.DurationInSeconds);
Description.Text = service.Description;
Discount.Text = Convert.ToString(service.Discount);
ServiceImage.Source = service.MainImagePathBitmap;
}
AddServiceButton.IsVisible = false;
2024-09-09 13:26:07 +00:00
Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
2024-09-05 13:11:39 +00:00
}
2024-09-06 14:13:21 +00:00
2024-09-10 13:12:58 +00:00
private async void AddPictureButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
2024-09-06 14:13:21 +00:00
{
2024-09-10 13:12:58 +00:00
/*OpenFileDialog openFileDialog = new OpenFileDialog();
var topLevel = await openFileDialog.ShowAsync(this);
*/
2024-09-06 14:13:21 +00:00
}
private void AddServiceButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
2024-09-09 13:26:07 +00:00
Service service = new Service()
{
Id = Utils.Context.Services.Count + 1,
Title = ServiceName.Text,
Cost = Convert.ToDecimal(Cost.Text),
DurationInSeconds = Convert.ToInt32(DurationInSeconds.Text),
Description = Description.Text,
Discount = Convert.ToDecimal(Discount.Text)
};
using (var context = new OvsyannikovContext())
{
context.Add(service);
context.SaveChanges();
}
Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
ServiceWindow serviceWindow = new(AdminMode);
serviceWindow.Show();
Close();
2024-09-06 14:13:21 +00:00
}
private void EditServiceButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
2024-09-13 07:39:04 +00:00
/*Service service = new Service()
2024-09-12 19:16:08 +00:00
{
Id = serviceID,
Title = ServiceName.Text,
Cost = Convert.ToDecimal(Cost.Text),
DurationInSeconds = Convert.ToInt32(DurationInSeconds.Text),
Description = Description.Text,
Discount = Convert.ToDecimal(Discount.Text)
2024-09-13 07:39:04 +00:00
};*/
2024-09-12 19:16:08 +00:00
using (var context = new OvsyannikovContext())
{
2024-09-13 07:39:04 +00:00
var service = context.Services.Find(serviceID);
service.Title = ServiceName.Text;
service.Cost = Convert.ToDecimal(Cost.Text);
service.DurationInSeconds = Convert.ToInt32(DurationInSeconds.Text);
service.Description = Description.Text;
service.Discount = Convert.ToDecimal(Discount.Text);
context.Update(service);
2024-09-12 19:16:08 +00:00
context.SaveChanges();
}
2024-09-13 07:39:04 +00:00
/*using (var context = new OvsyannikovContext())
{
context.Update(service);
context.SaveChanges();
}*/
2024-09-12 19:16:08 +00:00
Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
ServiceWindow serviceWindow = new(AdminMode);
serviceWindow.Show();
Close();
2024-09-06 14:13:21 +00:00
}
2024-09-05 13:11:39 +00:00
}