public async Task <IActionResult> Create([Bind("Title,Message,ContactType,Img")] ContactApplication contactApplication)
        {
            if (ModelState.IsValid)
            {
                // Init new Application:
                contactApplication.UserID     = (int)HttpContext.Session.GetInt32("UserID");
                contactApplication.CreateDate = DateTime.Today;
                contactApplication.Status     = Config.TransactionStatus.Open;

                if (contactApplication.Img != null)
                {
                    _context.Add(contactApplication);
                    await _context.SaveChangesAsync(); // Let this contact application get ID from SQL server

                    contactApplication.ImgPath = Image.Save(contactApplication.UserID, contactApplication.Img, getLastContactAppRegisteredID(), "ContactApplication");

                    _context.Update(contactApplication);
                    await _context.SaveChangesAsync(); // Update image path to this contact app ID
                }
                else // (ID is not needed yet because we are not saving an image)
                {
                    contactApplication.ImgPath = "No Image Attached";

                    _context.Add(contactApplication);
                    await _context.SaveChangesAsync(); // let this contact application get ID from SQL server
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(contactApplication));
        }
        public ContactController(ContactApplication contactApp)
        {
            if (contactApp == null)
            {
                throw new ArgumentNullException(nameof(contactApp));
            }

            _contactApp = contactApp;
        }
Exemplo n.º 3
0
        private ContactController ContactControllerTest(int qtd)
        {
            var srvCollection = new ServiceCollection();

            var mockRepo = new Mock <IContactRepositoy>();

            mockRepo.Setup(repo => repo.GetAll())
            .Returns(GetTestSessions(qtd).AsEnumerable());

            ConfigureMapp.Initialize(srvCollection);
            var mapp = DomainInjection.GetService <IMapper>(srvCollection);

            var srv = new ContactService(mockRepo.Object);
            var app = new ContactApplication(srv, mapp);

            var controller = new ContactController(app);

            return(controller);
        }
        public async Task <IActionResult> Edit(int id, [Bind("Title,Message,ContactType,Img,Status")] ContactApplication ContactAppAfterEdit)
        {
            var ContactAppBeforeEdit = await _context.ContactApplication.FindAsync(id);

            if (ContactAppBeforeEdit == null)
            {
                return(NotFound());
            }


            if (ModelState.IsValid)
            {
                try
                {
                    if (ContactAppAfterEdit.Img != null)
                    {
                        string newImgRelativePath = Image.Edit(ContactAppBeforeEdit.UserID, ContactAppBeforeEdit.ImgPath, ContactAppAfterEdit.Img, ContactAppBeforeEdit.ContactAppID, "ContactApplication");
                        ContactAppBeforeEdit.ImgPath = newImgRelativePath;
                    }

                    ContactAppBeforeEdit.Status      = ContactAppAfterEdit.Status;
                    ContactAppBeforeEdit.Title       = ContactAppAfterEdit.Title;
                    ContactAppBeforeEdit.Message     = ContactAppAfterEdit.Message;
                    ContactAppBeforeEdit.ContactType = ContactAppAfterEdit.ContactType;

                    _context.Update(ContactAppBeforeEdit);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ContactApplicationExists(ContactAppAfterEdit.ContactAppID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(ContactAppAfterEdit));
        }