Ejemplo n.º 1
0
        public async Task <ActionResult <EditStaffDto> > Create(CreateStaffDto targetValue)
        {
            if (string.IsNullOrWhiteSpace(targetValue.LastName) || string.IsNullOrWhiteSpace(targetValue.FirstName))
            {
                return(BadRequest());
            }

            var username = User.Identity.Name;
            var isAdmin  = User.IsInRole(Roles.Admin);

            var data = dataContext.Set <School>()
                       .Where(x => targetValue.Schools.Contains(x.Id))
                       .All(x => isAdmin ||
                            // the principal requesting must be among the staff at the school being requested
                            x.Staff
                            .SelectMany(y => y.Staff.Users)
                            .Any(z => z.UserName == username));

            if (!data)
            {
                return(BadRequest());
            }

            // this is an example of different DTOs serving different purposes
            // e.g. create does not specify the ID where edit staff should have the ID
            var result = dataContext.Set <Staff>().Add(new Staff
            {
                FirstName  = targetValue.FirstName,
                LastName   = targetValue.LastName,
                CreatedUtc = DateTimeOffset.UtcNow,
                // notice the '?.' symbol, that is the null conditional operator
                // see: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-
                // this will prevent an exception from being thrown if thar targetValue.Schools is null

                // this is also a random example of creating several entities at once using EF core
                // in this case we are effectively assigning a staff person to whatever school Ids are passed in
                Schools = targetValue.Schools?.Select(x => new SchoolStaff
                {
                    SchoolId = x
                }).ToList()
            });

            await dataContext.SaveChangesAsync();

            var savedEntity = result.Entity;

            return(Created($"/api/schools/{savedEntity.Id}", new EditStaffDto
            {
                FirstName = savedEntity.FirstName,
                LastName = savedEntity.LastName,
                Id = savedEntity.Id
            }));
        }
Ejemplo n.º 2
0
        public async Task <long> InsertStaff(CreateStaffDto model)
        {
            Staff staff;

            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            var user = await _userAppService.RetrieveUser(model.UserId);

            var checkStaff = await RetriveStaffByUserId(model.UserId);

            if (checkStaff != null)
            {
                return(0);
            }
            else
            {
                staff = new Staff
                {
                    UserId       = user.Id,
                    Firstname    = model.Firstname,
                    Middlename   = model.Middlename,
                    Lastname     = model.Lastname,
                    DateOfBirth  = model.DateOfBirth,
                    DateEmployed = model.DateEmployed,
                    Gender       = model.Gender,
                    UserType     = user.UserType,
                    IsUpdate     = true,
                    IsActive     = true,
                };
                await _schoolHubDbContext.Staff.AddAsync(staff);

                await _schoolHubDbContext.SaveChangesAsync();

                var nUser = await _schoolHubDbContext.User.Where(s => s.Id == user.Id).FirstOrDefaultAsync();

                if (nUser != null)
                {
                    nUser.IsUpdated = true;
                }
                ;
                _schoolHubDbContext.Entry(nUser).State = EntityState.Modified;
                await _schoolHubDbContext.SaveChangesAsync();
            }

            //INsert into UserStaffMap
            await _mappingService.MapStaffUser(staff.UserId, staff.Id);

            return(await Task.FromResult(staff.Id));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateStaff([FromBody] CreateStaffDto model)
        {
            try
            {
                var staff = await _staffAppService.InsertStaff(model);

                if (staff < 1)
                {
                    return(BadRequest("Staff could not be created"));
                }
                return(Ok(staff));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }