Exemple #1
0
        public async Task <ActionResult <DealerContact> > Insert([FromBody] DealerContact model)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            if (ValidateRequiredFields(model) == false)
            {
                return(BadRequest("Unable to create the Dealer. Required fields missing"));
            }

            var dealerContact = await(from c in _db.DealerContacts
                                      where c.ContactName == model.ContactName
                                      select c)
                                .AsNoTracking()
                                .FirstOrDefaultAsync();

            if (dealerContact != null)
            {
                return(StatusCode(409, "Unable to create the DealerContact . The ContactName already exists in Dealers"));
            }

            await _db.DealerContacts.AddAsync(model);

            await _db.SaveChangesAsync();

            return(StatusCode(201, "The DealerContact has been Created"));
        }
Exemple #2
0
        public static void DealerContactLogger(DealerContact dealerContact, string action, string screenName, string UserId)
        {
            string fullFilePath = GetFilePath(screenName);

            try
            {
                if (!File.Exists(fullFilePath))
                {
                    FileStream file = File.Create(fullFilePath);
                    file.Close();
                }

                using (StreamWriter writer = File.AppendText(fullFilePath))
                {
                    PrintDefaultText(writer, fullFilePath, action, screenName, UserId);

                    writer.WriteLine("DealerContactId: " + dealerContact.DealerContactId);
                    writer.WriteLine("DealerId: " + dealerContact.DealerId);
                    writer.WriteLine("DealerName: " + dealerContact.DealerName);
                    writer.WriteLine("DealerContactName: " + dealerContact.DealerContactName);
                    writer.WriteLine("Department: " + dealerContact.Department);
                    writer.WriteLine("Phone: " + dealerContact.Phone);
                    writer.WriteLine("CellPhone: " + dealerContact.CellPhone);
                    writer.WriteLine("Email: " + dealerContact.Email);
                    writer.WriteLine("EmCountryId: " + dealerContact.CountryId);
                    writer.WriteLine("-------------------------------------------------------------------------------");
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #3
0
        //[Route("")]
        public async Task <ActionResult <DealerContact> > Update(int id, [FromBody] DealerContact model)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            if (id != model.DealerContactId)
            {
                return(BadRequest());
            }

            if (ValidateRequiredFields(model) == false)
            {
                return(BadRequest("Unable to create the DealerContact. Required fields missing"));
            }

            var dealerContact = await(from c in _db.DealerContacts
                                      where c.DealerContactId != model.DealerContactId &&
                                      c.ContactName == model.ContactName
                                      select c)
                                .AsNoTracking()
                                .FirstOrDefaultAsync();

            if (dealerContact != null)
            {
                return(StatusCode(409, "Unable to update the DealerContact. The record already exists in DealerContacts"));
            }

            _db.DealerContacts.Update(model);
            await _db.SaveChangesAsync();

            return(Ok("Update successfull"));
        }
Exemple #4
0
        public static bool DealerContactDataValidation(DealerContact dealerContact)
        {
            bool IsEmpty = false;

            if ((dealerContact.DealerId == 0) || (dealerContact.CountryId == 0) || (dealerContact.DealerContactName == string.Empty) || (dealerContact.Department == string.Empty))
            {
                IsEmpty = true;
            }
            return(IsEmpty);
        }
Exemple #5
0
        private bool ValidateRequiredFields(DealerContact dealerContact)
        {
            if (dealerContact.DealerContactId <= 0 || dealerContact.MainDealerId <= 0 || dealerContact.ContactName == null)
            {
                return(false);
            }

            var mainDealerId = _db.Dealers
                               .AsNoTracking()
                               .FirstOrDefault(u => u.MainDealerId == dealerContact.MainDealerId);

            if (mainDealerId == null)
            {
                return(false);
            }
            return(true);
        }
Exemple #6
0
        public frmAddOrUpdateDealerContact(DealerContact dealerContact)
        {
            InitializeComponent();

            countryList = DALHelpers.GetCountries();
            dealerList  = DALHelpers.GetDealersByCountry(dealerContact.CountryId);

            DealerContactId = dealerContact.DealerContactId;

            hasTheSaveButtonPressed = false;

            int counter = 0;

            if (dealerContact.DealerContactId != 0)
            {
                foreach (Country country in countryList)
                {
                    cmbCountries.Items.Add(country.CountryName.ToString());

                    if (dealerContact.CountryId == country.CountryId)
                    {
                        cmbCountries.SelectedIndex = counter;
                        dealerList = DALHelpers.GetDealersByCountry(country.CountryId);
                    }

                    counter++;
                }

                counter = 0;

                foreach (Dealer dealer in dealerList)
                {
                    cmbDealerName.Items.Add(dealer.DealerName.ToString());

                    if (dealerContact.CountryId == dealer.CountryId)
                    {
                        cmbDealerName.SelectedIndex = counter;
                    }

                    counter++;
                }

                this.Text = "Update Dealer Contact";
                txtDealerContactName.Text = dealerContact.DealerContactName;
                txtDepartment.Text        = dealerContact.Department;
                txtPhone.Text             = dealerContact.Phone;
                txtCellPhone.Text         = dealerContact.CellPhone;
                txtEmail.Text             = dealerContact.Email;
            }
            else
            {
                foreach (Country country in countryList)
                {
                    cmbCountries.Items.Add(country.CountryName.ToString());
                }

                counter = 0;

                foreach (Dealer dealer in dealerList)
                {
                    cmbDealerName.Items.Add(dealer.DealerName.ToString());

                    if (dealerContact.CountryId == dealer.CountryId)
                    {
                        cmbDealerName.SelectedIndex = counter;
                    }

                    counter++;
                }

                this.Text = "New Dealer Contact";
            }

            cmbDealerName.Focus();
        }
Exemple #7
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            hasTheSaveButtonPressed = true;

            DealerContact dealerContact = new DealerContact();
            bool          ok            = false;

            dealerContact.DealerContactName = txtDealerContactName.Text;
            dealerContact.Department        = txtDepartment.Text;
            dealerContact.Phone             = txtPhone.Text;
            dealerContact.CellPhone         = txtCellPhone.Text;
            dealerContact.Email             = txtEmail.Text;

            foreach (Country country in countryList)
            {
                if (cmbCountries.Text == country.CountryName)
                {
                    dealerContact.CountryId = country.CountryId;
                }
            }

            foreach (Dealer dealer in dealerList)
            {
                if (cmbDealerName.Text == dealer.DealerName)
                {
                    dealerContact.DealerId = dealer.DealerId;
                }
            }

            if (Validation.DealerContactDataValidation(dealerContact))
            {
                MessageBox.Show("All fields are required.");
                return;
            }

            if (DealerContactId != 0)
            {
                dealerContact.DealerContactId = DealerContactId;
                //UPDATE
                ok = DALHelpers.UpdateDealerContact(dealerContact);

                if (ok == true)
                {
                    MessageBox.Show("The record was successfully saved!");
                }
                else
                {
                    MessageBox.Show("Error: An error has ocurred when trying to update the Dealer Contact!");
                }

                ActivityLog.DealerContactLogger(dealerContact, "UPDATE", "Dealer Contact", Environment.UserName);
                this.Close();
            }
            else //NEW CONTACT
            {
                ok = DALHelpers.AddDealerContact(dealerContact);

                if (ok == true)
                {
                    MessageBox.Show("The record was successfully saved!");
                }

                ActivityLog.DealerContactLogger(dealerContact, "CREATE", "Dealer Contact", Environment.UserName);
                this.Close();
            }
        }