финальный круд. интересно, под утро я начну материться в коммитах?

This commit is contained in:
antoc0der 2024-05-26 22:02:15 +04:00
parent 08d329df3f
commit 0964f598b9
6 changed files with 49 additions and 27 deletions

View File

@ -30,7 +30,7 @@ namespace VeterinaryBusinessLogic.OfficePackage
CreateTable(new List<string> { "4cm", "4cm", "4cm", "4cm" });
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Дата", "Название медикамента", "Услуга рекомендации","Название визита" },
Texts = new List<string> { "Дата", "Название медикамента", "Посещение ","Лекарство" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});

View File

@ -45,7 +45,7 @@ namespace VeterinaryDatabaseImplement.Implements
return null;
}
using var context = new VeterinaryDatabase();
return context.Drugs
return context.Drugs.Include(x => x.Doctor)
.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrugName) &&
@ -81,7 +81,7 @@ namespace VeterinaryDatabaseImplement.Implements
drug.UpdateMedications(context, model); // работает
context.SaveChanges();
drug.Update(context,model);// не работает сумма
drug.Update(model);// не работает сумма
context.SaveChanges();
transaction.Commit();
return drug.GetViewModel;

View File

@ -24,6 +24,8 @@ namespace VeterinaryDatabaseImplement.Models
public int Count { get; set; }
[Required]
public int DoctorId { get; private set; }
public virtual Doctor? Doctor { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
private Dictionary<int, IMedicationModel>? _drugMedications = null;
@ -69,19 +71,10 @@ namespace VeterinaryDatabaseImplement.Models
};
}
// цену считает неверно
public void Update(VeterinaryDatabase context, DrugBindingModel model)
public void Update( DrugBindingModel model)
{
var justMedications = model.DrugMedications.Select(x => new DrugMedication
{
Medication = context.Medications.First(y => y.Id == x.Key)
}).ToList();
double pric = 0;
foreach (var justMedication in justMedications)
{
pric += justMedication.Medication.Price;
}
DrugName = model.DrugName;
Price = pric;
}
public DrugViewModel GetViewModel => new()
{
@ -90,11 +83,18 @@ namespace VeterinaryDatabaseImplement.Models
Price = Price,
Count= Count,
DoctorId=DoctorId,
DateCreate = DateCreate
DateCreate = DateCreate,
DrugMedications = DrugMedications
};
public void UpdateMedications(VeterinaryDatabase context, DrugBindingModel model)
{
var drugMedications = context.DrugMedications.Where(rec => rec.DrugId == model.Id).ToList();
var list = new List <int>();
foreach (var rec in model.DrugMedications)
{
list.Add(rec.Key);
}
//var tmp = model.DrugMedications;
if (drugMedications != null && drugMedications.Count > 0)
{
// из таблицы бд удаляем строчки, которые были отменены(которые раньше были а тепперь их в модели нет)
@ -123,6 +123,17 @@ namespace VeterinaryDatabaseImplement.Models
context.SaveChanges();
}
_drugMedications = null;
var justMedications = list.Select(x => new DrugMedication
{
Medication = context.Medications.First(y => y.Id == x)
}).ToList();
double pric = 0;
foreach (var justMedication in justMedications)
{
pric += justMedication.Medication.Price;
}
Price = pric;
context.SaveChanges();
}

View File

@ -19,18 +19,20 @@ namespace VeterinaryRestApi.Controllers
_drug = drug;
}
[HttpGet]
public Tuple<DrugViewModel, List<string>>? GetDrug(int drugId)
public Tuple<DrugViewModel, List<Tuple<string, int>>>? GetDrug(int drugId)
{
try
{
var elem = _drug.ReadElement(new DrugSearchModel { Id = drugId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList());
var res = Tuple.Create(elem, elem.DrugMedications.Select(x => Tuple.Create(x.Value.MedicationName, x.Value.Id)).ToList());
res.Item1.DrugMedications = null!;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения лекарства по id={Id}", drugId);
_logger.LogError(ex, "Ошибка получения услуги по id={Id}", drugId);
throw;
}
}
@ -52,15 +54,22 @@ namespace VeterinaryRestApi.Controllers
}
[HttpGet]
public List<DrugViewModel> GetDrugs(int doctorId)
public List<DrugViewModel> GetDrugs(int? doctorId = null)
{
try
{
return _drug.ReadList(new DrugSearchModel { DoctorId = doctorId });
List<DrugViewModel> res;
if (!doctorId.HasValue)
res = _drug.ReadList(null);
else
res = _drug.ReadList(new DrugSearchModel { DoctorId = doctorId.Value });
foreach (var drug in res)
drug.DrugMedications = null;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка лекарств");
_logger.LogError(ex, "Ошибка получения списка услуг");
throw;
}
}

View File

@ -314,13 +314,13 @@ namespace VeterinaryShowDoctorApp.Controllers
}
[HttpGet]
public Tuple<DrugViewModel, List<string>>? GetDrug(int drugId)
public Tuple<DrugViewModel, List<Tuple<string, int>>>? GetDrug(int drugId)
{
if (APIDoctor.Doctor == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
var result = APIDoctor.GetRequest<Tuple<DrugViewModel, List<string>>>($"api/drug/getdrug?drugid={drugId}");
var result = APIDoctor.GetRequest<Tuple<DrugViewModel, List<Tuple<string, int>>>>($"api/drug/getdrug?drugid={drugId}");
if (result == null)
{
return default;

View File

@ -33,7 +33,7 @@
<select name="medications" class="form-control" multiple size="5" id="medications">
@foreach (var medication in ViewBag.Medications)
{
<option value="@medication.Id" id="@medication.MedicationName">@medication.MedicationName</option>
<option value="@medication.Id" data-name="@medication.Id">@medication.MedicationName</option>
}
</select>
</div>
@ -44,6 +44,8 @@
</div>
</form>
@section Scripts
{
<script>
@ -58,11 +60,11 @@
success: function (result) {
console.log(result.item2);
$('#name').val(result.item1.drugName);
$('#price').val(result.item1.price);
$('#count').val(result.item1.count);
$('#price').val(result.item1.price);
$.map(result.item2, function (n) {
console.log("#" + n);
$("#" + n).attr("selected", "selected")
$(`option[data-name=${n.item2}]`).attr("selected", "selected")
});
}
@ -74,4 +76,4 @@
check();
});
</script>
}
}