Example #1
0
        public async Task <StaffDto> AddStaff(StaffForCreationDto staff)
        {
            var employeeJson =
                new StringContent(JsonSerializer.Serialize(staff), Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync("api/staff", employeeJson);

            if (response.IsSuccessStatusCode)
            {
                return(await JsonSerializer.DeserializeAsync <StaffDto>(await response.Content.ReadAsStreamAsync()));
            }

            return(null);
        }
Example #2
0
        public async Task <ActionResult <StaffDto> > CreateStaff(StaffForCreationDto staff)
        {
            var staffEntity = _mapper.Map <Staff>(staff);

            //first Add staff
            _staffRepo.AddStaff(staffEntity);
            await _staffRepo.SaveAsync();

            var createdStaffId = staffEntity.Id;

            if (staff.RoleIds != null)
            {
                _staffRepo.AssignRoleCollectionToStaff(createdStaffId, staff.RoleIds);
                await _staffRepo.SaveAsync();

                if (staff.HouseId != 0)
                {
                    if (await _staffRepo.IsHeadOfHouseAsync(createdStaffId))
                    {
                        _staffRepo.AddHouseToStaff(createdStaffId, staff.HouseId);
                    }
                    else
                    {
                        return(BadRequest("StaffMember must have Role HeadOfHouse to assign House"));
                    }
                }
                if (staff.CourseIds.Any())
                {
                    if (await _staffRepo.IsTeacherAsync(createdStaffId))
                    {
                        _staffRepo.AssignCourseCollectionToStaff(createdStaffId, staff.CourseIds);
                    }
                    else
                    {
                        return(BadRequest("Staffmember must have role Teacher to assign Courses"));
                    }
                }
            }
            //if all goes well
            await _staffRepo.SaveAsync();

            var staffWithRoles = await _staffRepo.GetStaffByIdAsync(staffEntity.Id);

            return(CreatedAtRoute("GetStaffMember",
                                  new { staffId = createdStaffId },
                                  _mapper.Map <StaffDto>(staffWithRoles)));
        }