<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Update2ProducerUserInfo : DbMigration
    {
        public override void Up()
        {
            AlterColumn("dbo.ProducerUserInfo", "FirstName", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerUserInfo", "LastName", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerUserInfo", "Address1", c => c.String(maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "Address2", c => c.String(maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "City", c => c.String(maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "State", c => c.String(maxLength: 2));
            AlterColumn("dbo.ProducerUserInfo", "ZipCode", c => c.String(maxLength: 5));
            AlterColumn("dbo.ProducerUserInfo", "PrimaryPhoneType", c => c.String());
            AlterColumn("dbo.ProducerUserInfo", "PrimaryPhoneNumber", c => c.String(maxLength: 16));
            AlterColumn("dbo.ProducerUserInfo", "SecondaryPhoneType", c => c.String());
        }
        
        public override void Down()
        {
            AlterColumn("dbo.ProducerUserInfo", "SecondaryPhoneType", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerUserInfo", "PrimaryPhoneNumber", c => c.String(nullable: false, maxLength: 16));
            AlterColumn("dbo.ProducerUserInfo", "PrimaryPhoneType", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerUserInfo", "ZipCode", c => c.String(nullable: false, maxLength: 5));
            AlterColumn("dbo.ProducerUserInfo", "State", c => c.String(nullable: false, maxLength: 2));
            AlterColumn("dbo.ProducerUserInfo", "City", c => c.String(nullable: false, maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "Address2", c => c.String(nullable: false, maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "Address1", c => c.String(nullable: false, maxLength: 50));
            AlterColumn("dbo.ProducerUserInfo", "LastName", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerUserInfo", "FirstName", c => c.String(nullable: false, maxLength: 30));
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using DAL;
    using Models;
    using Models.Enums;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<ProducerVerification.Web.DAL.ProducerVerificationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ProducerVerificationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //context.Producers.AddOrUpdate(p => p.AgencyName, new ProducerInfo()
            //{
            //    AgencyName = "<NAME>",
            //    BusinessName = "<NAME>",
            //    EmailAddress = "<EMAIL>",
            //    PrimaryPhoneNumber = "123",
            //    PrimaryPhoneType = PhoneType.Business,
            //    PreferredPaymentMethod = CommissionPaymentMethod.ACH
            //});

        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class NonEmptyAddressFields : DbMigration
    {
        public override void Up()
        {
            AlterColumn("dbo.ProducerInfo", "MailingStreet", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "MailingCity", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "MailingState", c => c.String(nullable: false, maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "MailingZipCode", c => c.String(nullable: false, maxLength: 41));
            AlterColumn("dbo.ProducerInfo", "PhysicalStreet", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "PhysicalCity", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "PhysicalState", c => c.String(nullable: false, maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "PhysicalZipCode", c => c.String(nullable: false, maxLength: 41));
            AlterColumn("dbo.ProducerInfo", "BillingStreet", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "BillingCity", c => c.String(nullable: false, maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "BillingState", c => c.String(nullable: false, maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "BillingZipCode", c => c.String(nullable: false, maxLength: 41));
        }
        
        public override void Down()
        {
            AlterColumn("dbo.ProducerInfo", "BillingZipCode", c => c.String(maxLength: 41));
            AlterColumn("dbo.ProducerInfo", "BillingState", c => c.String(maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "BillingCity", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "BillingStreet", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "PhysicalZipCode", c => c.String(maxLength: 41));
            AlterColumn("dbo.ProducerInfo", "PhysicalState", c => c.String(maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "PhysicalCity", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "PhysicalStreet", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "MailingZipCode", c => c.String(maxLength: 41));
            AlterColumn("dbo.ProducerInfo", "MailingState", c => c.String(maxLength: 4));
            AlterColumn("dbo.ProducerInfo", "MailingCity", c => c.String(maxLength: 30));
            AlterColumn("dbo.ProducerInfo", "MailingStreet", c => c.String(maxLength: 30));
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class MoreProducerFields : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Address",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        StreetNameHouseNumber = c.String(),
                        City = c.String(),
                        State = c.String(),
                        ZipCode = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            AddColumn("dbo.ProducerInfo", "MailingAddressId", c => c.Int(nullable: false));
            AddColumn("dbo.ProducerInfo", "PrimaryPhoneType", c => c.Int(nullable: false));
            AddColumn("dbo.ProducerInfo", "SecondaryPhoneType", c => c.Int(nullable: false));
            AlterColumn("dbo.ProducerInfo", "Name", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerInfo", "BusinessName", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerInfo", "PrimaryPhoneNumber", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerInfo", "EmailAddress", c => c.String(nullable: false));
            CreateIndex("dbo.ProducerInfo", "MailingAddressId");
            AddForeignKey("dbo.ProducerInfo", "MailingAddressId", "dbo.Address", "Id", cascadeDelete: true);
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.ProducerInfo", "MailingAddressId", "dbo.Address");
            DropIndex("dbo.ProducerInfo", new[] { "MailingAddressId" });
            AlterColumn("dbo.ProducerInfo", "EmailAddress", c => c.String());
            AlterColumn("dbo.ProducerInfo", "PrimaryPhoneNumber", c => c.String());
            AlterColumn("dbo.ProducerInfo", "BusinessName", c => c.String());
            AlterColumn("dbo.ProducerInfo", "Name", c => c.String());
            DropColumn("dbo.ProducerInfo", "SecondaryPhoneType");
            DropColumn("dbo.ProducerInfo", "PrimaryPhoneType");
            DropColumn("dbo.ProducerInfo", "MailingAddressId");
            DropTable("dbo.Address");
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class ShortenedStreetColumnName : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.ProducerInfo", "MailingStreet", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "PhysicalStreet", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "BillingStreet", c => c.String(maxLength: 30));
            DropColumn("dbo.ProducerInfo", "MailingStreetNameHouseNumber");
            DropColumn("dbo.ProducerInfo", "PhysicalStreetNameHouseNumber");
            DropColumn("dbo.ProducerInfo", "BillingStreetNameHouseNumber");
        }
        
        public override void Down()
        {
            AddColumn("dbo.ProducerInfo", "BillingStreetNameHouseNumber", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "PhysicalStreetNameHouseNumber", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "MailingStreetNameHouseNumber", c => c.String(maxLength: 30));
            DropColumn("dbo.ProducerInfo", "BillingStreet");
            DropColumn("dbo.ProducerInfo", "PhysicalStreet");
            DropColumn("dbo.ProducerInfo", "MailingStreet");
        }
    }
}
<file_sep>﻿using ProducerVerification.Web.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace ProducerVerification.Web.DAL
{
    public class ProducerVerificationDbContext : DbContext
    {
        public ProducerVerificationDbContext() : base("ProducerVerificationDbContext") { }


        //Tables
        public DbSet<ProducerInfo> Producers { get; set; }
        public DbSet<AuthenticationCodes> AuthenticationCodes { get; set; }
        //public DbSet<Address> Addresses { get; set; }



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            
        }

        public System.Data.Entity.DbSet<ProducerVerification.Web.Models.ProducerUserInfo> ProducerUserInfoes { get; set; }
    }
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProducerVerification.Domain.Models
{
    class ProducerInfo
    {
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class ConsolidatedAddresses : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.ProducerInfo", "MailingAddressId", "dbo.Address");
            DropIndex("dbo.ProducerInfo", new[] { "MailingAddressId" });
            AddColumn("dbo.ProducerInfo", "ProducerCode", c => c.String(nullable: false, maxLength: 8));
            AddColumn("dbo.ProducerInfo", "AgencyName", c => c.String(nullable: false));
            AddColumn("dbo.ProducerInfo", "MailingStreetNameHouseNumber", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "MailingCity", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "MailingState", c => c.String(maxLength: 4));
            AddColumn("dbo.ProducerInfo", "MailingZipCode", c => c.String(maxLength: 41));
            AddColumn("dbo.ProducerInfo", "PhysicalStreetNameHouseNumber", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "PhysicalCity", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "PhysicalState", c => c.String(maxLength: 4));
            AddColumn("dbo.ProducerInfo", "PhysicalZipCode", c => c.String(maxLength: 41));
            AddColumn("dbo.ProducerInfo", "BillingStreetNameHouseNumber", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "BillingCity", c => c.String(maxLength: 30));
            AddColumn("dbo.ProducerInfo", "BillingState", c => c.String(maxLength: 4));
            AddColumn("dbo.ProducerInfo", "BillingZipCode", c => c.String(maxLength: 41));
            AddColumn("dbo.ProducerInfo", "BankName", c => c.String(nullable: false));
            AddColumn("dbo.ProducerInfo", "RoutingNumber", c => c.String(nullable: false));
            AddColumn("dbo.ProducerInfo", "LastFourAccountNumber", c => c.String(nullable: false));
            AddColumn("dbo.ProducerInfo", "ExpirationYear", c => c.Int(nullable: false));
            AlterColumn("dbo.ProducerInfo", "DoingBusinessAs", c => c.String(maxLength: 30));
            DropColumn("dbo.ProducerInfo", "Name");
            DropColumn("dbo.ProducerInfo", "MailingAddressId");
            DropTable("dbo.Address");
        }
        
        public override void Down()
        {
            CreateTable(
                "dbo.Address",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        StreetNameHouseNumber = c.String(),
                        City = c.String(),
                        State = c.String(),
                        ZipCode = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            AddColumn("dbo.ProducerInfo", "MailingAddressId", c => c.Int(nullable: false));
            AddColumn("dbo.ProducerInfo", "Name", c => c.String(nullable: false));
            AlterColumn("dbo.ProducerInfo", "DoingBusinessAs", c => c.String());
            DropColumn("dbo.ProducerInfo", "ExpirationYear");
            DropColumn("dbo.ProducerInfo", "LastFourAccountNumber");
            DropColumn("dbo.ProducerInfo", "RoutingNumber");
            DropColumn("dbo.ProducerInfo", "BankName");
            DropColumn("dbo.ProducerInfo", "BillingZipCode");
            DropColumn("dbo.ProducerInfo", "BillingState");
            DropColumn("dbo.ProducerInfo", "BillingCity");
            DropColumn("dbo.ProducerInfo", "BillingStreetNameHouseNumber");
            DropColumn("dbo.ProducerInfo", "PhysicalZipCode");
            DropColumn("dbo.ProducerInfo", "PhysicalState");
            DropColumn("dbo.ProducerInfo", "PhysicalCity");
            DropColumn("dbo.ProducerInfo", "PhysicalStreetNameHouseNumber");
            DropColumn("dbo.ProducerInfo", "MailingZipCode");
            DropColumn("dbo.ProducerInfo", "MailingState");
            DropColumn("dbo.ProducerInfo", "MailingCity");
            DropColumn("dbo.ProducerInfo", "MailingStreetNameHouseNumber");
            DropColumn("dbo.ProducerInfo", "AgencyName");
            DropColumn("dbo.ProducerInfo", "ProducerCode");
            CreateIndex("dbo.ProducerInfo", "MailingAddressId");
            AddForeignKey("dbo.ProducerInfo", "MailingAddressId", "dbo.Address", "Id", cascadeDelete: true);
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class MoreSetupForPaymentMethodEnumToString : DbMigration
    {
        public override void Up()
        {
            DropColumn("dbo.ProducerInfo", "PreferredPaymentMethod");
        }
        
        public override void Down()
        {
            AddColumn("dbo.ProducerInfo", "PreferredPaymentMethod", c => c.Int(nullable: false));
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class InitWithProducerInfo : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.ProducerInfo",
                c => new
                    {
                        ID = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                        BusinessName = c.String(),
                        DoingBusinessAs = c.String(),
                        PrimaryPhoneNumber = c.String(),
                        SecondaryPhoneNumber = c.String(),
                        EmailAddress = c.String(),
                        Fax = c.String(),
                        SeparateAccountEntityName = c.String(),
                        PreferredPaymentMethod = c.Int(nullable: false),
                        IpAddress = c.String(),
                    })
                .PrimaryKey(t => t.ID);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.ProducerInfo");
        }
    }
}
<file_sep>﻿using ProducerVerification.Web.DAL;
using ProducerVerification.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;

namespace ProducerVerification.Web.Controllers
{
    public class HomeController : Controller
    {
        private ProducerVerificationDbContext db = new ProducerVerificationDbContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Home(ProducerUserInfo model)
        {
            if (model.ProducerCode == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            model.ProducerCode = model.ProducerCode.Trim();
            model.AuthorizationCode = model.AuthorizationCode.Trim();

            ProducerInfo producerInfo = db.Producers
                .Where(p => p.ProducerCode == model.ProducerCode)
                .FirstOrDefault();

            if (producerInfo == null)
            {
                return HttpNotFound();
            }

            bool isAuthorized = AuthenticationCodes.VerifyAuthCode(db, model.ProducerCode, model.AuthorizationCode);

            if (!isAuthorized)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            return RedirectToAction("Upload", model);
        }

        public ActionResult Upload(ProducerUserInfo model)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileUpload, ProducerUserInfo model)
        {
            if( fileUpload != null && fileUpload.ContentLength > 0)
            {
                var name = model.ProducerCode + Path.GetFileName(fileUpload.FileName);
                //var path = Path.Combine(Server.MapPath("~App_Data/uploads"), name);
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/uploads/", name);

                var ext = Path.GetExtension(name);

                if (ext != ".xlsm")
                {
                    TempData["error"] = "Please select a valid file.";
                    return View();
                }

                fileUpload.SaveAs(path);
                TempData["error"] = "Thank you for your time.";
                return RedirectToAction("Index");
            }
            else
            {
                TempData["error"] = "Please select a valid file.";
            }
            return View();
        }

        public FilePathResult Download()
        {
            var name = ConfigurationManager.AppSettings["ExcelDownload"];
            //var path = Path.Combine(Server.MapPath("~App_Data/downloads"), name);
            //var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), name);

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/downloads/", name);
            return File(path, "application/octet-stream", name);
        }

    }
}<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class AddedAuthCodeTable : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.AuthenticationCodes",
                c => new
                    {
                        ProducerCode = c.String(nullable: false, maxLength: 128),
                        AuthenticationCode = c.String(),
                    })
                .PrimaryKey(t => t.ProducerCode);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.AuthenticationCodes");
        }
    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class InitProducerUserInfo : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.ProducerUserInfo",
                c => new
                    {
                        RecID = c.Int(nullable: false, identity: true),
                        ProducerCode = c.String(nullable: false, maxLength: 8),
                        UserCode = c.String(nullable: false, maxLength: 150),
                        ConfirmUserCode = c.String(nullable: false, maxLength: 150),
                        FirstName = c.String(nullable: false, maxLength: 30),
                        LastName = c.String(nullable: false, maxLength: 30),
                        Address1 = c.String(nullable: false, maxLength: 50),
                        Address2 = c.String(nullable: false, maxLength: 50),
                        City = c.String(nullable: false, maxLength: 50),
                        State = c.String(nullable: false, maxLength: 2),
                        ZipCode = c.String(nullable: false, maxLength: 5),
                        PrimaryPhoneType = c.String(nullable: false),
                        PrimaryPhoneNumber = c.String(nullable: false, maxLength: 16),
                        SecondaryPhoneType = c.String(nullable: false),
                        SecondaryPhoneNumber = c.String(maxLength: 16),
                        FaxNumber = c.String(maxLength: 16),
                        EmailVerified = c.Boolean(nullable: false),
                    })
                .PrimaryKey(t => t.RecID);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.ProducerUserInfo");
        }
    }
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace ProducerVerification.Web.Models
{
    public class ProducerUserInfo
    {
        
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int RecID { get; set; }

        [Required]
        [MaxLength(8)]
        [DisplayName("Producer Code")]
        public string ProducerCode { get; set; }

        [Required]
        [MaxLength(150)]
        [EmailAddress]
        [DisplayName("Email Address")]
        public string UserCode { get; set; }

        [NotMapped]
        //[MaxLength(150)]
        //[EmailAddress]
        //[Compare("UserCode", ErrorMessage = "The email and confirmation email do not match.")]
        [DisplayName("Confirm Email Address")]
        public string ConfirmUserCode { get; set; }

        [NotMapped]
        [DisplayName("Business Name")]
        public string BusinessName { get; set; }

        [NotMapped]
        [DisplayName("Authorization Code")]
        public string AuthorizationCode { get; set; }

        [MaxLength(30)]
        [DisplayName("First Name")]
        public string FirstName { get; set; }

        [MaxLength(30)]
        [DisplayName("Last Name")]
        public string LastName { get; set; }

        [MaxLength(50)]
        [DisplayName("Business Address")]
        public string Address1 { get; set; }

        [MaxLength(50)]
        [DisplayName("Business Address")]
        public string Address2 { get; set; }

        [MaxLength(50)]
        [DisplayName("City")]
        public string City { get; set; }

        [MaxLength(2)]
        [DisplayName("State")]
        public string State { get; set; }

        [MaxLength(5)]
        [DisplayName("Zip Code")]
        public string ZipCode { get; set; }

        public string PrimaryPhoneType { get; set; }

        [MaxLength(16)]
        [Phone]
        [DisplayName("Primary Phone")]
        public string PrimaryPhoneNumber { get; set; }

        public string SecondaryPhoneType { get; set; }

        [MaxLength(16)]
        [Phone]
        [DisplayName("Secondary Phone")]
        public string SecondaryPhoneNumber { get; set; }

        [MaxLength(16)]
        [Phone]
        [DisplayName("Fax Number")]
        public string FaxNumber { get; set; }

        public bool EmailVerified { get; set; }


    }
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ProducerVerification.Web.DAL;
using ProducerVerification.Web.Models;
using System.Web.Mail;
using System.Net.Mail;
using System.Configuration;
using System.Web.Services.Description;
using ProducerVerification.Web.Helpers;
using System.Text.RegularExpressions;

namespace ProducerVerification.Web.Controllers
{
    public class ProducerUserInfoController : Controller
    {
        private ProducerVerificationDbContext db = new ProducerVerificationDbContext();

        // GET: ProducerUserInfo
        public ActionResult Index(int? done)
        {
            if(done == 1)
            {
                TempData["error"] = "Thank you for your time.";
            }
            
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(ProducerUserInfo model)
        {
            if (model.ProducerCode == null)
            {
                TempData["error"] = "The Producer Code you entered is not valid.";
                return RedirectToAction("Index", "ProducerUserInfo");
            }

            model.ProducerCode = model.ProducerCode.Trim();
            model.AuthorizationCode = model.AuthorizationCode.Trim();

            ProducerInfo producerInfo = db.Producers
                .Where(p => p.ProducerCode == model.ProducerCode)
                .FirstOrDefault();

            if (producerInfo == null)
            {
                TempData["error"] = "The Producer Code you entered is not valid.";
                return RedirectToAction("Index", "ProducerUserInfo");
            }

            bool isAuthorized = AuthenticationCodes.VerifyAuthCode(db, model.ProducerCode, model.AuthorizationCode);

            if (!isAuthorized)
            {
                TempData["error"] = "The Authorization Code you entered is not valid.";
                return RedirectToAction("Index", "ProducerUserInfo");
            }

            //if you want users to have a unique email address 
            //int uniqueEmail = db.ProducerUserInfoes.Count(e => e.UserCode == model.UserCode);

            //if (uniqueEmail > 0)
            //{
            //    TempData["error"] = "The email address you entered already exists. Please use another.";
            //    return RedirectToAction("Index", "ProducerUserInfo");
            //}

            if (model.ConfirmUserCode != model.UserCode)
            {
                TempData["error"] = "The Confirm Email Address and Email Address must match.";
                return RedirectToAction("Index", "ProducerUserInfo");
            }

            var emailUrl = "https://" + Request.Url.Authority.ToString();

            if (ModelState.IsValid)
            {
                db.ProducerUserInfoes.Add(model);
                db.SaveChanges();
                SendEmail(emailUrl, model.RecID, model.UserCode);
                TempData["error"] = "A confirmation email has been sent to you. Please refer to it for next steps.";
                return RedirectToAction("Index", "ProducerUserInfo");
            }

            return View(model);
        }

        // GET: ProducerUserInfo/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            byte[] eID = Convert.FromBase64String(id);
            int rID = BitConverter.ToInt32(eID, 0);
            var verify = 0;

            ProducerUserInfo producerUserInfo = db.ProducerUserInfoes.Find(rID);

            if (producerUserInfo == null)
            {
                return RedirectToAction("Edit", "ProducerUserInfo", TempData["error"] = "The Producer Code you entered is not valid.");
            }
            else
            {
                verify = 1;
            }

            ProducerInfo producerInfo = db.Producers
            .Where(p => p.ProducerCode == producerUserInfo.ProducerCode)
            .FirstOrDefault();

            producerUserInfo.BusinessName = producerInfo.BusinessName;
            producerUserInfo.Address1 = producerInfo.MailingStreet;
            producerUserInfo.City = producerInfo.MailingCity;
            producerUserInfo.State = producerInfo.MailingState;
            producerUserInfo.ZipCode = producerInfo.MailingZipCode;

            if (verify == 1)
            {
                producerUserInfo.EmailVerified = true;
            } else
            {
                producerUserInfo.EmailVerified = false;
            }

            return View(producerUserInfo);
        }

        // POST: ProducerUserInfo/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "RecID,ProducerCode,UserCode,FirstName,LastName,Address1,Address2,City,State,ZipCode,PrimaryPhoneType,PrimaryPhoneNumber,SecondaryPhoneType,SecondaryPhoneNumber,FaxNumber,EmailVerified")] ProducerUserInfo producerUserInfo)
        {
            byte[] bytes = BitConverter.GetBytes(producerUserInfo.RecID);
            string s = Convert.ToBase64String(bytes);

            if (ModelState.IsValid)
            {
                db.Entry(producerUserInfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new { id = s });
            } 
            return View(producerUserInfo);
        }
        
        // GET: ProducerUserInfo/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            byte[] eID = Convert.FromBase64String(id);
            int rID = BitConverter.ToInt32(eID, 0);
            ViewBag.id = id;

            ProducerUserInfo producerUserInfo = db.ProducerUserInfoes.Find(rID);

            if (producerUserInfo == null)
            {
                return HttpNotFound();
            }

            ProducerInfo producerInfo = db.Producers
            .Where(p => p.ProducerCode == producerUserInfo.ProducerCode)
            .FirstOrDefault();

            producerUserInfo.BusinessName = producerInfo.BusinessName;

            return View(producerUserInfo);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        public static void SendEmail(string urlFormat, int recID, string userCode)
        {
            byte[] bytes = BitConverter.GetBytes(recID);
            string s = Convert.ToBase64String(bytes);
            var confirmLink = string.Format(urlFormat + "/ProducerUserInfo/Edit/" + s);
            var bodyBuilder = new System.Text.StringBuilder();
            bodyBuilder.Append("Hello, <br/><br/>");
            bodyBuilder.Append("<a href=\"");
            bodyBuilder.Append(confirmLink);
            bodyBuilder.Append("\">");
            bodyBuilder.Append("Click here ");
            bodyBuilder.Append("</a>");
            bodyBuilder.Append("to verify your email address and continue to next steps. <br/><br/>");
            bodyBuilder.Append("NCJUA");

            var mail = new System.Net.Mail.MailMessage();
            mail.From = new MailAddress(ConfigurationManager.AppSettings["SmtpUser"]);
            mail.Subject = "NCJUA-NCIUA Producer Data Collection Confirmation Email Address";
            mail.Body = bodyBuilder.ToString();
            mail.IsBodyHtml = true;

            mail.To.Add(new MailAddress(userCode));

            var client = new SmtpClient()
            {
                Host = ConfigurationManager.AppSettings["SmtpHost"],
                UseDefaultCredentials = false,
                DeliveryMethod = SmtpDeliveryMethod.Network
            };            

            client.Send(mail);
        }

    }
}
<file_sep>namespace ProducerVerification.Web.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Update1ProducerUserInfo : DbMigration
    {
        public override void Up()
        {
            DropColumn("dbo.ProducerUserInfo", "ConfirmUserCode");
        }
        
        public override void Down()
        {
            AddColumn("dbo.ProducerUserInfo", "ConfirmUserCode", c => c.String(nullable: false, maxLength: 150));
        }
    }
}
