public async Task <ActionResult> Put(int id, [FromBody] RFQDTO rfqDto) { if (rfqDto == null || rfqDto.RFQId != id) { return(await Task.Run(() => BadRequest())); } var orgItem = _context.RFQs.SingleOrDefault(o => o.RFQId == id); if (orgItem == null) { return(await Task.Run(() => NotFound())); } _mapper.Map(rfqDto, orgItem, opt => { opt.ConfigureMap() .ForMember(tgt => tgt.TargetedProduct, op => op.Ignore()) .ForMember(tgt => tgt.SelectedEdition, op => op.Ignore()); }); // orgItem.Address = rfqDto.Address; // orgItem.CompanyArabicName = rfqDto.CompanyArabicName; // orgItem.CompanyEnglishName = rfqDto.CompanyEnglishName; // orgItem.ContactPersonArabicName = rfqDto.ContactPersonArabicName; // orgItem.ContactPersonEmail = rfqDto.ContactPersonEmail; // orgItem.ContactPersonEnglishName = rfqDto.ContactPersonEnglishName; // orgItem.ContactPersonMobile = rfqDto.ContactPersonMobile; // orgItem.ContactPersonPosition = rfqDto.ContactPersonPosition; // orgItem.Location = rfqDto.Location; // orgItem.PhoneNumber = rfqDto.PhoneNumber; // orgItem.SelectedEditionId = rfqDto.SelectedEditionId; // orgItem.Status = rfqDto.Status; // orgItem.SubmissionTime = DateTime.Now; // orgItem.TargetedProductId = rfqDto.TargetedProductId; // orgItem.UniversalIP = rfqDto.UniversalIP; // orgItem.Website = rfqDto.Website; // _context.RFQs.Update(orgItem); _context.SaveChanges(); // var _rfq = _context.RFQs // .Where(r => r.RFQId == rfqDto.RFQId) // .Include(r => r.SelectedEdition) // .Include(r => r.TargetedProduct) // .SingleOrDefault(); // rfqDto = _mapper.Map(_rfq, rfqDto); return(await Task.Run(() => new ObjectResult(rfqDto))); }
public async Task <ActionResult> Post([FromBody] RFQDTO rfqDto) { if (rfqDto == null) { return(await Task.Run(() => BadRequest())); } bool saved = false; try { rfqDto.RFQCode = DateTime.Now.Ticks.ToString(); rfqDto.SubmissionTime = DateTime.Now; var rfq = _mapper.Map <RFQ>(rfqDto); _context.RFQs.Add(rfq); _context.SaveChanges(); rfqDto = _mapper.Map(rfq, rfqDto); saved = true; } catch (System.Exception) { saved = false; throw; } if (!saved) { return(await Task.Run(() => new NoContentResult())); } if (rfqDto.SendEmail) { bool sent = false; try { var htmlTemplate = ""; var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "MailTemplete.html"); using (var file = new StreamReader(path)) { htmlTemplate = file.ReadToEnd(); } string[] tags = new[] { "Name", "phone" }; string[] tagValues = new[] { rfqDto.ContactPersonEnglishName, rfqDto.ContactPersonMobile }; MailHelper.sendMail(new MailData { Message = new MailMessageData(new[] { rfqDto.ContactPersonEmail }) { Body = MailHelper.MessageBody(htmlTemplate, tags, tagValues) }, SMTP = new SmtpData() }); sent = true; } catch (Exception ex) { sent = false; throw ex; } if (sent) { RFQAction rfqAction = new RFQAction { ActionCode = DateTime.Now.Ticks.ToString(), ActionTime = DateTime.Now, ActionType = ActionType.EmailMessage, Comments = "Automated Email", RepresentativeId = 0, SubmissionTime = DateTime.Now, UniversalIP = rfqDto.UniversalIP }; var newRfq = _context.RFQs.Where(r => r.RFQId == rfqDto.RFQId).Include(r => r.RFQActions).SingleOrDefault(); newRfq.RFQActions.Add(rfqAction); _context.SaveChanges(); } } var _rfq = _context.RFQs .Where(r => r.RFQId == rfqDto.RFQId) .Include(r => r.SelectedEdition) .Include(r => r.TargetedProduct) .SingleOrDefault(); rfqDto = _mapper.Map(_rfq, rfqDto); return(await Task.Run(() => new ObjectResult(rfqDto))); }