123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using Avalonia.Controls.Platform;
- using AvaloniaApplicationTest.Models;
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static System.Runtime.InteropServices.JavaScript.JSType;
- namespace AvaloniaApplicationTest.ViewModels
- {
- public class AddPageViewModel : ReactiveObject
- {
- bool edit = false;
- Hotel new_hotel = new Hotel();
- public AddPageViewModel()
- {
- }
- public AddPageViewModel(int ID) //я не понимаю, каким образом Авалония берет айди именно предпоследнего элемента. Именно элемента
- {
- edit = true;
- new_hotel = MainWindowViewModel.myConnection.Hotels.FirstOrDefault(x=>x.Id == ID);
- if (new_hotel != null) {
- Name = new_hotel.Name;
- Description = new_hotel.Description;
- CountOfStars = new_hotel.CountOfStars;
- ValueSelect = MainWindowViewModel.myConnection.Countries.First(x => x.Code == new_hotel.CountryCode); ;
- }
- else
- {
- Message = "Что-то пошло не так. Нажмите кнопку Отмена";
- }
- }
- public string Name
- {
- get => name;
- set => this.RaiseAndSetIfChanged(ref name, value);
- }
- public string Description
- {
- get => description;
- set => this.RaiseAndSetIfChanged(ref description, value);
- }
- public int CountOfStars
- {
- get => count_of_stars;
- set => this.RaiseAndSetIfChanged(ref count_of_stars, value);
- }
- public List<Country> CBContries
- {
- get => countries;
- set => this.RaiseAndSetIfChanged(ref countries, value);
- }
- public Country ValueSelect
- {
- get => data;
- set
- {
- this.RaiseAndSetIfChanged(ref data, value);
- }
- }
- public string Message
- {
- get => m;
- set
- {
- this.RaiseAndSetIfChanged(ref m, value);
- }
- }
- List<Country> countries = MainWindowViewModel.myConnection.Countries.ToList();
- string name = "";
- int count_of_stars = 0;
- string description = "";
- Country data = MainWindowViewModel.myConnection.Countries.First();
- string m = "";
- void GetNewData()
- {
- if (!edit) new_hotel.Id = MainWindowViewModel.myConnection.Hotels.Max(x => x.Id)+1;
- new_hotel.Name = Name;
- new_hotel.CountOfStars = CountOfStars;
- new_hotel.CountryCode = ValueSelect.Code;
- new_hotel.Description = Description;
- }
- bool CheckData()
- {
- if ((Name == string.Empty) || (Description == string.Empty) || (ValueSelect.Name == string.Empty) || (CountOfStars < 0) || (CountOfStars > 5))
- {
- Message = "Некорректные данные. Повторите";
- return false;
- }
- return true;
- }
- public void Save()
- {
- GetNewData();
- if (CheckData())
- {
- Message = string.Empty;
- try
- {
- if (!edit) MainWindowViewModel.myConnection.Hotels.Add(new_hotel);
- MainWindowViewModel.myConnection.SaveChanges();
- Message = "Сохранено. Нажмите кнопку Отмена.";
- }
- catch
- {
- Message = "Что-то пошло не так!";
- }
- }
- }
- }
- }
|