public IActionResult EditInvoice(int invoice_id, [FromBody] InvoiceAjaxModel invoiceModel) { try { var userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId == invoiceModel.CreatedBy.Id) //only user who creted the invoice can edit it { using (DBEntities dbe = new DBEntities()) { //find invoice Invoice invoice = dbe.Invoices.SingleOrDefault(i => i.Id == invoice_id); invoice.Name = invoiceModel.Name; invoice.Description = invoiceModel.Description; invoice.FiatAmount = invoiceModel.FiatAmount; invoice.FiatCurrencyCode = invoiceModel.FiatCurrencyCode; invoice.ExchangeRateMode = invoiceModel.ExchangeRateMode; dbe.Invoices.Update(invoice); dbe.SaveChanges(); return(Ok()); } } else { return(Unauthorized()); } } catch (Exception ex) { return(BadRequest(ex)); } }
public IActionResult CreateInvoice([FromBody] InvoiceAjaxModel model) { try { if (ModelState.IsValid) { var userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); using (DBEntities dbe = new DBEntities()) { User loggedUser = dbe.Users.SingleOrDefault(u => u.Id == userId); Invoice invoice = new Invoice() { CreatedBy = dbe.Users.SingleOrDefault(u => u.Id == userId), DateCreated = DateTime.UtcNow, InvoiceGuid = Guid.NewGuid(), State = (int)InvoiceState.NOT_PAID, Name = model.Name, Description = model.Description, Recipient = model.Recipient, FiatCurrencyCode = model.FiatCurrencyCode, ExchangeRateMode = model.ExchangeRateMode, ExchangeRateSetTime = null, FiatAmount = model.FiatAmount, FileName = model.FileName, File = model.File }; // Proccess uploaded file if (!string.IsNullOrEmpty(invoice.File)) { string[] fileInfo = invoice.File.Split(';'); string mimeType = fileInfo[0].Split(':')[1]; string fileContent = fileInfo[1].Split(',')[1]; FileData fileData = new FileData() { FileName = invoice.InvoiceGuid.ToString() + Path.GetExtension(invoice.FileName), FileContent = Convert.FromBase64String(fileContent), }; WebDAVClient client = new WebDAVClient(env, configuration); invoice.File = client.UploadFile(fileData); invoice.FileMime = mimeType; } foreach (string cc in model.Accept) { string CC = cc.ToUpper(); // Check if exchange rate should be calculated now, or when the recipent opens payment page double?exchangeRate = invoice.ExchangeRateMode == "invoice" ? currencyConfiguration.Adapters[CC].GetExchangeRate(invoice.FiatCurrencyCode) : (double?)null; invoice.PaymentsAvailable.Add(new InvoicePayment() { CurrencyCode = CC, VarSymbol = currencyConfiguration.Adapters[CC].GetVarSymbol(), ExchangeRate = exchangeRate }); } dbe.Invoices.Add(invoice); dbe.SaveChanges(); foreach (string cc in model.Accept) { currencyConfiguration.Adapters[cc.ToUpper()].GetAddress(invoice.Id, loggedUser); } // send info e-mail string invoiceUrl = string.Format("{0}/invoice/{1}", env.IsDevelopment() ? configuration["FrontEndHostName:Development"] : configuration["FrontEndHostName:Production"], invoice.InvoiceGuid); string subject = $"New invoice from {loggedUser.UserName}"; string attachment = !string.IsNullOrEmpty(invoice.File) ? $"{invoice.File}|{invoice.FileName}|{invoice.FileMime}" : ""; string body = System.IO.File.ReadAllText("wwwroot/web-api-static/templates/email/invoice.html"); body = body.Replace("{User.Name}", loggedUser.UserName) .Replace("{Invoice.Name}", invoice.Name) .Replace("{Invoice.Description}", invoice.Description) .Replace("{URL}", invoiceUrl); EmailSender sender = new EmailSender(configuration); Email email = sender.CreateEmailEntity("*****@*****.**", invoice.Recipient, body, subject, attachment); sender.AddEmailToQueue(email); //Front end needs this new id to call GetInvoice return(Created("/api/invoices/" + invoice.InvoiceGuid, invoice.InvoiceGuid)); } } else { var query = from state in ModelState.Values from error in state.Errors select error.ErrorMessage; var errors = query.ToList(); string allErrors = ""; foreach (string error in errors) { allErrors += error + "\n"; } return(BadRequest(allErrors)); } } catch (Exception ex) { return(BadRequest(ex)); } }