public void AddSpending(SpendingDto model) { //var cat = db.Categories.FirstOrDefault(x => x.Name == model.Category) ?? new Category { Name = model.Category }; var category = db.Categories.FirstOrDefault(x => x.Name == model.Category); if (category == null) { category = new Category() { Name = model.Category }; db.Categories.Add(category); } var spending = new Spending { Category = category, Value = model.Value, Date = model.Date, Description = model.Description, Tags = new List <SpendingTag>() }; foreach (var t in model.Tags) { var tag = db.Tags.FirstOrDefault(x => x.Name == t); if (tag == null) { tag = new Tag() { Name = t }; } spending.Tags.Add(new SpendingTag { Tag = tag }); } // added user //var user = db.Users.FirstOrDefault(x => x.Id == model.UserId); //spending.User = user; var claims = HttpContext.User.Claims; var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; spending.UserId = userId; // db.Spendings.Add(spending); db.SaveChanges(); }
// Fait manuellement ici mais pour un gros projet une librairie comme automapper ou a minima l'extension MappingGenerator serait plus pertinent. public static SpendingDto ToSpendingDto(this Spending spending) { SpendingDto returnValue = new SpendingDto(); returnValue.Amount = spending.Amount; returnValue.Comment = spending.Comment; returnValue.DateInUtc = spending.DateInUtc; returnValue.ISOCurrencySymbol = spending.ISOCurrencySymbol; returnValue.Nature = Enum.GetName(typeof(Nature), spending.Nature); returnValue.UserFirstName = spending.User.FirstName; returnValue.UserLastName = spending.User.LastName; return(returnValue); }
public async Task <OperationResult> SaveAsync(SpendingDto model, Guid userId) { var result = new OperationResult(); var account = await accountRepository.All().OrderByDescending(x => x.CreatedOn).FirstOrDefaultAsync(); //Last info about account try { var spending = await spendingRepository.All().FirstOrDefaultAsync(x => x.Id == model.Id); if (spending == null) { var newSpec = mapper.Map <Spending>(model); account.TotalCash -= model.Money; account.Kefir -= model.Kefir; account.Oil -= model.Oil; account.Salt -= model.Salt; account.Eggs -= model.Eggs; account.Vanila -= model.Vanila; account.Sugar -= model.Sugar; account.Soda -= model.Soda; spendingRepository.Add(newSpec); } else { account.TotalCash -= (model.Money - spending.Money); account.Kefir -= (model.Kefir - spending.Kefir); //литр account.Oil -= (model.Oil - spending.Oil); // литр account.Salt -= (model.Salt - spending.Salt); // кг account.Eggs -= (model.Eggs - spending.Eggs); // штук account.Vanila -= (model.Vanila - spending.Vanila); // грамм account.Sugar -= (model.Sugar - spending.Sugar); // кг account.Soda -= (model.Soda - spending.Soda); //грамм mapper.Map(model, spending); } await spendingRepository.SaveChangesAsync(userId); } catch (Exception ex) { logger.LogError($"Exception: {ex.GetType()}; Сообщение об ошибке: {ex.Message}; StackTrace: {ex.StackTrace}"); } return(result); }
public async Task <IActionResult> SaveData(SpendingDto model) { var result = await spendingService.SaveAsync(model, this.User.GetUserId()); return(RedirectToAction("GetAllSpendings", "Spending")); }
public AddSpending() { InitializeComponent(); Spending = new SpendingDto(); DataContext = Spending; }