Example #1
0
        public async Task <IActionResult> PutStaffNames(int id, StaffNames staffNames)
        {
            if (id != staffNames.Id)
            {
                return(BadRequest());
            }

            _context.Entry(staffNames).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StaffNamesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Department,BookingCount")] StaffNames staffNames)
        {
            if (id != staffNames.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(staffNames);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StaffNamesExists(staffNames.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(staffNames));
        }
        /// <summary>
        /// Loads the staff names from the text file.
        /// </summary>
        /// <returns>list of staff names</returns>
        private List <StaffNames> LoadStaffNamesFromFile()
        {
            string[] lines = { };

            //Gets or sets the absolute path to the directory that contains the web-servable application content files.
            string webRootPath = _environment.WebRootPath;

            FileInfo filePath = new FileInfo(Path.Combine(webRootPath, "Staff.txt"));

            lines = System.IO.File.ReadAllLines(filePath.ToString());

            //holds all the staff names
            Dictionary <string, StaffNames> StaffnamesDict = new Dictionary <string, StaffNames>();

            List <StaffNames> AllStaffNames = new List <StaffNames>();

            foreach (var name in lines)
            {
                StaffNames staffnames = new StaffNames();
                string[]   data       = name.Split(',');
                staffnames.Name       = data[0];
                staffnames.Department = data[1];

                //cut out duplicate names
                //if the name isn't there, add it in
                if (!StaffnamesDict.ContainsKey(staffnames.Name))
                {
                    StaffnamesDict.Add(staffnames.Name, staffnames);
                    AllStaffNames.Add(staffnames);
                }
            }

            return(AllStaffNames);
        }
Example #4
0
        public async Task <ActionResult <StaffNames> > PostStaffNames(StaffNames staffNames)
        {
            _context.StaffNames.Add(staffNames);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetStaffNames", new { id = staffNames.Id }, staffNames));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Department,BookingCount")] StaffNames staffNames)
        {
            if (ModelState.IsValid)
            {
                _context.Add(staffNames);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(staffNames));
        }
        public async Task <IActionResult> PostStaffNames([FromBody] StaffNames staffNames)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.StaffNames.Add(staffNames);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetStaffNames", new { id = staffNames.Id }, staffNames));
        }
Example #7
0
        [Consumes("application/json")] //https://github.com/aspnet/AspNetCore/issues/4396
        public async Task <ActionResult <StaffNames> > PostStaffNames([FromBody] StaffNamesFormDTO staffNamesForm)
        {
            //when importing from formdata is always comes in as string
            StaffNames staffNames = new StaffNames();

            staffNames.Id           = Convert.ToInt32(staffNamesForm.Id);
            staffNames.Name         = staffNamesForm.Name;
            staffNames.Department   = staffNamesForm.Department;
            staffNames.VisitorCount = Convert.ToInt32(staffNamesForm.VisitorCount);



            _context.StaffNames.Add(staffNames);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetStaffNames", new { id = staffNames.Id }, staffNames));
        }
        public IEnumerable <StaffNames> RemoveDuplicateStaff()
        {
            //holds all the staff names
            Dictionary <string, StaffNames> StaffnamesDict = new Dictionary <string, StaffNames>();
            List <StaffNames> AllStaffNames = new List <StaffNames>();

            foreach (var name in _context.StaffNames.Distinct().ToList())
            //.Select(x => new { x.Name, x.Department })
            {
                //have to add each unique name to the dictionary
                StaffNames staffnames = new StaffNames();
                staffnames.Name       = name.Name;
                staffnames.Department = name.Department;
                staffnames.Id         = name.Id;
                //added here
                if (!StaffnamesDict.ContainsKey(staffnames.Name))
                {
                    StaffnamesDict.Add(staffnames.Name, staffnames);
                    AllStaffNames.Add(staffnames);
                }
            }
            //pull out all the existing names from the application user db
            //Empty the Database to remove old values

            foreach (var staff in _context.StaffNames.ToList())
            {
                _context.StaffNames.Remove(staff);
            }
            _context.SaveChanges();

            //empty the identity table

            foreach (var staff in _applicationDbContext.Users.ToList())
            {
                _applicationDbContext.Users.Remove(staff);
            }
            _applicationDbContext.SaveChanges();

            //save the filtered data
            //_context.AddRange(AllStaffNames);
            //_context.SaveChanges();


            return(AllStaffNames);
        }
Example #9
0
        public void UploadStaffFile()
        {
            //--------------Get the data from the Text file --------------
            string[] lines = { };

            //Gets or sets the absolute path to the directory that contains the web-servable application content files.
            string webRootPath = _environment.WebRootPath;

            FileInfo filePath = new FileInfo(Path.Combine(webRootPath, "Staff.txt"));

            lines = System.IO.File.ReadAllLines(filePath.ToString());

            //holds all the staff names
            Dictionary <string, StaffNames> StaffnamesDict = new Dictionary <string, StaffNames>();

            List <StaffNames> AllStaffNames = new List <StaffNames>();

            foreach (var name in lines)
            {
                StaffNames staffnames = new StaffNames();
                string[]   data       = name.Split(',');
                staffnames.Name       = data[0];
                staffnames.Department = data[1];

                //if the name isn't there, add it in
                if (!StaffnamesDict.ContainsKey(staffnames.Name))
                {
                    StaffnamesDict.Add(staffnames.Name, staffnames);
                    AllStaffNames.Add(staffnames);
                }
            }


            //--------------Get the data from the Database --------------

            //add in the ones from the db
            AllStaffNames.AddRange(RemoveDuplicateStaff());
            //sort the new data and pull out only uniques
            IOrderedEnumerable <StaffNames> orderedStaffNames = AllStaffNames.Distinct().OrderBy(
                i => i.Department).ThenBy(i => i.Name);

            _context.AddRange(orderedStaffNames);
            _context.SaveChanges();
        }
        public IActionResult Create([Bind("Id,Name,Department,VisitorCount")] StaffNamesDTO staffNamesDTO)
        {
            var staffNames = new StaffNames()
            {
                Id           = staffNamesDTO.Id,
                Name         = staffNamesDTO.Name,
                Department   = staffNamesDTO.Department,
                VisitorCount = staffNamesDTO.VisitorCount
            };



            if (ModelState.IsValid)
            {
                _context.Add(staffNames);
                _context.SaveChangesAsync();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(staffNamesDTO));
        }
Example #11
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Business,DateIn,DateOut")] Visitor visitor)
        {
            visitor.DateIn  = DateTime.Now;
            visitor.DateOut = DateTime.Now;

            StaffNames myNames = new StaffNames();

            myNames.Id           = 4;
            myNames.Department   = "asasa";
            myNames.Name         = "Howard the axe killer";
            myNames.VisitorCount = 2;

            visitor.StaffName = myNames;

            if (ModelState.IsValid)
            {
                _context.Add(visitor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(visitor));
        }
Example #12
0
        public IEnumerable <StaffNames> RemoveDuplicateStaff()
        {
            //holds all the staff names
            Dictionary <string, StaffNames> StaffnamesDict = new Dictionary <string, StaffNames>();
            List <StaffNames> AllStaffNames = new List <StaffNames>();

            foreach (var name in _context.StaffNames.Distinct().ToList())
            //.Select(x => new { x.Name, x.Department })
            {
                //have to remove
                StaffNames staffnames = new StaffNames();
                staffnames.Name       = name.Name;
                staffnames.Department = name.Department;
                staffnames.Id         = name.Id;
                if (!StaffnamesDict.ContainsKey(staffnames.Name))
                {
                    StaffnamesDict.Add(staffnames.Name, staffnames);
                    AllStaffNames.Add(staffnames);
                }
            }

            //Empty the Database to remove old values

            foreach (var staff in _context.StaffNames.ToList())
            {
                _context.StaffNames.Remove(staff);
            }
            _context.SaveChanges();

            //save the filtered data
            //_context.AddRange(AllStaffNames);
            //_context.SaveChanges();


            return(AllStaffNames);
        }