Exemplo n.º 1
0
        /// <summary>
        /// Save new contact information
        /// </summary>
        /// <param name="model">contact view model</param>
        /// <returns>unique identifier code</returns>
        public int SaveContact(ContactViewModel model)
        {
            JSONReadWrite           readWrite = new JSONReadWrite();
            List <ContactViewModel> contacts  = JsonConvert.DeserializeObject <List <ContactViewModel> >(readWrite.Read("contacts.json", "data"));

            if (contacts == null)
            {
                contacts = new List <ContactViewModel>();
            }

            // get lastly inserted index- Id
            int index = contacts.Max(c => c.Id);

            // Increment by one
            model.Id = index + 1;

            // add to collection
            contacts.Add(model);

            // write to json
            string jSONString = JsonConvert.SerializeObject(contacts);

            readWrite.Write("contacts.json", "data", jSONString);

            return(model.Id);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get contact with unique identifier
        /// </summary>
        /// <param name="id">unique identifier code</param>
        /// <returns>existing contact</returns>
        public ContactViewModel GetById(int id)
        {
            List <ContactViewModel> contacts  = new List <ContactViewModel>();
            JSONReadWrite           readWrite = new JSONReadWrite();

            contacts = JsonConvert.DeserializeObject <List <ContactViewModel> >(readWrite.Read("contacts.json", "data"));

            return(contacts.FirstOrDefault(x => x.Id == id));
        }
Exemplo n.º 3
0
        /// <summary>
        /// List all contacts
        /// </summary>
        /// <returns>contacts</returns>
        public List <ContactViewModel> ListContacts()
        {
            JSONReadWrite           readWrite = new JSONReadWrite();
            List <ContactViewModel> contacts  = JsonConvert.DeserializeObject <List <ContactViewModel> >(readWrite.Read("contacts.json", "data"));


            if (contacts == null)
            {
                contacts = new List <ContactViewModel>();
            }

            return(contacts);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Delete existing contact information
        /// </summary>
        /// <param name="id">unique identifier code</param>
        /// <returns>Is delete flag</returns>
        public bool DeleteContact(int id)
        {
            List <ContactViewModel> contacts  = new List <ContactViewModel>();
            JSONReadWrite           readWrite = new JSONReadWrite();

            contacts = JsonConvert.DeserializeObject <List <ContactViewModel> >(readWrite.Read("contacts.json", "data"));

            // find by id and remove record
            int index = contacts.FindIndex(x => x.Id == id);

            contacts.RemoveAt(index);

            // write to json
            string jSONString = JsonConvert.SerializeObject(contacts);

            readWrite.Write("contacts.json", "data", jSONString);

            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Update existing contact information
        /// </summary>
        /// <param name="model">contact view model</param>
        /// <returns>unique identifier</returns>
        public int UpdateContact(ContactViewModel model)
        {
            JSONReadWrite           readWrite = new JSONReadWrite();
            List <ContactViewModel> contacts  = JsonConvert.DeserializeObject <List <ContactViewModel> >(readWrite.Read("contacts.json", "data"));

            if (contacts == null)
            {
                contacts = new List <ContactViewModel>();
            }

            // find by id and update record
            int index = contacts.FindIndex(x => x.Id == model.Id);

            contacts[index] = model;

            // write to json
            string jSONString = JsonConvert.SerializeObject(contacts);

            readWrite.Write("contacts.json", "data", jSONString);

            return(model.Id);
        }