示例#1
0
        /// <summary>
        /// Create a new staff member from a given staff member object and return their new allocted database ID
        /// </summary>
        /// <param name="staffMember">the staff member data to save in the database</param>
        /// <returns>the integer identifier of the new staff entry</returns>
        public int Create(DataObject.StaffMember staffMember)
        {
            using (var dbContext = new AllocationContext(_dbOptions))
            {
                // Find if this staff member's designation is already known
                Model.Designation designation = dbContext.Designations
                                                .SingleOrDefault(d => String.Equals(d.Name, staffMember.Designation, StringComparison.OrdinalIgnoreCase));

                if (designation == null)
                {
                    throw new ArgumentException(String.Format("Unrecognised designation: {0}", staffMember.Designation));
                }

                // Now dig out the skills we already know about. We could ask for only the skills that this
                // staff member has, but then we need to check those skills against the staff member's with
                // case insensitive comparison on the DB's side. It's probably both faster and simpler to read
                // in all the skills and then filter on our side
                IDictionary <string, Model.Skill> dbSkills = new Dictionary <string, Model.Skill>(dbContext.Skills.ToDictionary(s => s.Name, s => s),
                                                                                                  StringComparer.OrdinalIgnoreCase);

                // Create the new staff member entry
                StaffMember dbStaffMember = new StaffMember()
                {
                    FirstName   = staffMember.FirstName,
                    LastName    = staffMember.LastName,
                    Alias       = staffMember.Alias,
                    Designation = designation,
                    LastDouble  = staffMember.LastDouble,
                    PhotoId     = staffMember.PhotoId,
                    RosterOnId  = staffMember.RosterOnId,
                };

                // Now generate their skill entries
                var skills = new List <StaffSkill>();
                foreach (string skillName in staffMember.Skills)
                {
                    if (!dbSkills.ContainsKey(skillName))
                    {
                        throw new ArgumentException(String.Format("Unrecognised skill: {0}", skillName));
                    }

                    skills.Add(new StaffSkill()
                    {
                        StaffMember = dbStaffMember, Skill = dbSkills[skillName]
                    });
                }
                dbStaffMember.StaffSkills = skills;

                // Finally, stick them in the database
                dbContext.StaffMembers.Add(dbStaffMember);
                dbContext.SaveChanges();

                // EF Core should magically populate this property after the commit
                return(dbStaffMember.StaffMemberId);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:HospitalAllocation.Data.StaffMember.IdentifiedStaffMember"/> class.
 /// </summary>
 /// <param name="staffId">The database identifier of the staff member</param>
 /// <param name="staffMember">The staff member value</param>
 public IdentifiedStaffMember(int staffId, StaffMember staffMember)
 {
     StaffId     = staffId;
     StaffMember = staffMember;
 }
示例#3
0
        /// <summary>
        /// Update an existing staff member in the database with the new values assigned
        /// </summary>
        /// <param name="staffId">the database ID of the staff member to update</param>
        /// <param name="updatedStaffMember">the new values to assign to the given staff member</param>
        /// <returns></returns>
        public DataObject.StaffMember Update(int staffId, DataObject.StaffMember updatedStaffMember)
        {
            using (var dbContext = new AllocationContext(_dbOptions))
            {
                StaffMember staffMember = dbContext.StaffMembers
                                          .Include(sm => sm.Designation)
                                          .Include(sm => sm.StaffSkills)
                                          .ThenInclude(ss => ss.Skill)
                                          .SingleOrDefault(sm => sm.StaffMemberId == staffId);

                // Save a lot of work if we don't recognise the ID
                if (staffMember == null)
                {
                    throw new ArgumentException(String.Format("Unrecognised staff ID: {0}", staffId));
                }

                staffMember.FirstName  = updatedStaffMember.FirstName;
                staffMember.LastName   = updatedStaffMember.LastName;
                staffMember.Alias      = updatedStaffMember.Alias;
                staffMember.LastDouble = updatedStaffMember.LastDouble;
                staffMember.RosterOnId = updatedStaffMember.RosterOnId;
                staffMember.PhotoId    = updatedStaffMember.PhotoId;

                // If we have a new designation, we need to ask the database
                if (updatedStaffMember.Designation != staffMember.Designation.Name)
                {
                    Model.Designation designation = dbContext.Designations
                                                    .SingleOrDefault(d => String.Equals(d.Name, updatedStaffMember.Designation, StringComparison.OrdinalIgnoreCase));
                    staffMember.Designation = designation ?? throw new ArgumentException(String.Format("Unrecognised designation: {0}", updatedStaffMember.Designation));
                }

                // Work out what skills have been changed
                var currentSkills = new HashSet <string>(staffMember.StaffSkills.Select(ss => ss.Skill.Name));
                var updatedSkills = new HashSet <string>(updatedStaffMember.Skills);

                // Skills that this staff member has that they didn't before
                IEnumerable <string> addedSkills = updatedSkills.Except(currentSkills, StringComparer.OrdinalIgnoreCase);
                // Skills this staff member used to have but now doesn't
                IEnumerable <string> deletedSkills = currentSkills.Except(updatedSkills, StringComparer.OrdinalIgnoreCase);

                // Remove all the deleted skills from the staff member
                staffMember.StaffSkills
                .RemoveAll(s => deletedSkills.Contains(s.Skill.Name, StringComparer.OrdinalIgnoreCase));

                // Add the new skills
                if (addedSkills.Any())
                {
                    // Query the database for all the skills (figure this is faster than a query per skill)
                    IDictionary <string, Model.Skill> skills = new Dictionary <string, Model.Skill>(dbContext.Skills.ToDictionary(s => s.Name, s => s),
                                                                                                    StringComparer.OrdinalIgnoreCase);

                    // If there are any skills we're trying to add by name that aren't in the database,
                    // we have a problem and cannot update
                    IEnumerable <string> unknownSkills = addedSkills.Except(skills.Keys, StringComparer.OrdinalIgnoreCase);
                    if (unknownSkills.Any())
                    {
                        throw new ArgumentException(String.Format("Unrecognised skills: {0}", String.Join(",", unknownSkills)));
                    }

                    // Finally add all the new skills in with new relations
                    staffMember.StaffSkills.AddRange(addedSkills.Select(s => new StaffSkill()
                    {
                        Skill       = skills[s],
                        StaffMember = staffMember
                    }));
                }

                // If we've gotten this far, we commit and are done
                dbContext.SaveChanges();

                return(new DataObject.StaffMember(staffMember.FirstName,
                                                  staffMember.LastName,
                                                  staffMember.Alias,
                                                  staffMember.Designation.Name,
                                                  staffMember.StaffSkills.Select(ss => ss.Skill.Name).ToList(),
                                                  staffMember.LastDouble,
                                                  null,
                                                  staffMember.PhotoId,
                                                  staffMember.RosterOnId));
            }
        }