示例#1
0
        public IActionResult CreateEmployeeForCompany(Guid companyId, [FromBody] EmployeeCreationDto employee)
        {
            if (employee == null)
            {
                _logger.LogError($"EmployeeCreationDto object sent from client is null");
                return(BadRequest("EmployeeCreationDtoObject is null"));
            }

            var company = _repository.Company.GetCompany(companyId, false);

            if (company == null)
            {
                _logger.LogError($"Company with id: {companyId} doesn't exist in the database.");
                return(NotFound());
            }

            var employeeEntity = _mapper.Map <Employee>(employee);

            _repository.Employee.CreateEmployeeForCompany(companyId, employeeEntity);
            _repository.Save();

            var employeeToReturn = _mapper.Map <EmployeeDto>(employeeEntity);

            return(CreatedAtRoute("GetEmployeeForCompany", new { companyId, id = employeeToReturn.Id }, employeeToReturn));
        }
        public HttpResponseMessage AddEmployee([FromBody] EmployeeCreationDto employeeCreationDto)
        {
            try
            {
                // check the valdation of the date
                if (employeeCreationDto.EmployeeBirthdate >= DateTime.Today)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Birthday less than today"));
                }
                // check the model state
                if (!ModelState.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Values));
                }

                using (var ctx = new ClientContext(webUrl))
                {
                    //check Permissions
                    //ClientResult<BasePermissions> permissions = ctx.Web.Lists.GetByTitle(listTitle).GetUserEffectivePermissions(userName);
                    //ctx.ExecuteQuery();
                    //Boolean hasPermission = permissions.Value.Has(PermissionKind.AddListItems);
                    //if (!hasPermission)
                    //    return Request.CreateResponse(HttpStatusCode.Unauthorized, "you're not allow to add employee");

                    //create item list
                    List oList = ctx.Web.Lists.GetByTitle(listTitle);

                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    ListItem oListItem = oList.AddItem(itemCreateInfo);
                    foreach (var property in typeof(EmployeeCreationDto).GetProperties())
                    {
                        if (property.Name != "ProfileURL" & property.Name != "Description")
                        {
                            oListItem[property.Name] = property.GetValue(employeeCreationDto);
                        }
                    }
                    FieldUrlValue fieldUrl = new FieldUrlValue();
                    fieldUrl.Url            = employeeCreationDto.ProfileURL;
                    fieldUrl.Description    = employeeCreationDto.Description;
                    oListItem["ProfileURL"] = fieldUrl;
                    oListItem.Update();
                    ctx.ExecuteQuery();
                    return(Request.CreateResponse(HttpStatusCode.Created));
                }
            }
            catch (Exception ex)
            {
                // return the error back
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }