Exemple #1
0
        public async Task <int> CreateAsync(EmployeeRequestModel model)
        {
            var vCard = new VCard()
            {
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                JobTitle     = model.JobTitle,
                Organization = model.VCard.Organization,
                Phone        = model.VCard.Phone,
                Street       = model.VCard.Street,
                PostalCode   = model.VCard.PostalCode,
                City         = model.VCard.City,
                Country      = model.VCard.Country,
                Email        = model.VCard.Email,
                HomePage     = model.VCard.HomePage,
                Mobile       = model.VCard.Mobile
            };

            var employee = new Data.Models.Employee
            {
                Description = model.Description,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                ImageUrl    = model.ImageUrl,
                JobTitle    = model.JobTitle,
                VCard       = vCard
            };

            await _dbContext.AddAsync(employee);

            await _dbContext.SaveChangesAsync();

            foreach (var activity in model.Activities)
            {
                var act = await _dbContext
                          .Activities
                          .FirstOrDefaultAsync(a => a.Id == activity.Id);

                var emp = await _dbContext
                          .Employees
                          .FirstOrDefaultAsync(e => e.Id == employee.Id);

                if (act == null || emp == null)
                {
                    continue;
                }
                var empAct = new EmployeeActivity
                {
                    Activity   = act,
                    ActivityId = act.Id,
                    Employee   = emp,
                    EmployeeId = emp.Id
                };

                await _dbContext.AddAsync(empAct);
            }

            await _dbContext.SaveChangesAsync();

            return(employee.Id);
        }