/// <summary>
        /// <inheritdoc />
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task <SkillCategory> AddSkillCategoryAsync(AddSkillCategoryViewModel model,
                                                                        CancellationToken cancellationToken = default(CancellationToken))
        {
            // Get request profile.
            var profile = _profileService.GetProfile(_httpRequestMessage);
            var userId  = model.UserId;

            if (profile.Role != UserRoles.Admin)
            {
                userId = profile.Id;
            }

            var skillCategories = _unitOfWork.SkillCategories.Search();
            var skillCategory   =
                await skillCategories.FirstOrDefaultAsync(x => x.Name.Equals(model.Name) && x.UserId == userId, CancellationToken.None);

            if (skillCategory != null)
            {
                throw new HttpException((int)HttpStatusCode.Conflict, HttpMessages.SkillCategoryAlreadyAvailable);
            }

            skillCategory        = new SkillCategory();
            skillCategory.Name   = model.Name;
            skillCategory.UserId = model.UserId;

            // Photo is defined. Save photo to path.
            if (model.Photo != null)
            {
                var relativeProfileImagePath = await _fileService.AddFileToDirectory(model.Photo.Buffer,
                                                                                     _appPath.ProfileImage, null, CancellationToken.None);

                skillCategory.Photo = _urlHelper.Content(relativeProfileImagePath);
            }

            if (model.Photo != null)
            {
                skillCategory.Photo = Convert.ToBase64String(model.Photo.Buffer);
            }
            skillCategory.CreatedTime = DateTime.Now.ToOADate();

            //Save to db context
            _unitOfWork.SkillCategories.Insert(skillCategory);

            //save change to db
            await _unitOfWork.CommitAsync();

            return(skillCategory);
        }
        public async Task <IHttpActionResult> AddSkillCategory([FromBody] AddSkillCategoryViewModel model)
        {
            if (model == null)
            {
                model = new AddSkillCategoryViewModel();
                Validate(model);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var skillCategory = await _skillSkilCategoryDomain.AddSkillCategoryAsync(model);

            return(Ok(skillCategory));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> AddSkillCategory([FromBody] AddSkillCategoryViewModel model)
        {
            //Check null for model
            if (model == null)
            {
                model = new AddSkillCategoryViewModel();
                Validate(model);
            }

            // Validate for model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var skillCategory = await _skillCategoryService.AddSkillCategoryAsync(model);

            return(Ok(skillCategory));
        }
Exemplo n.º 4
0
        /// <summary>
        ///     <inheritdoc />
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <SkillCategory> AddSkillCategoryAsync(AddSkillCategoryViewModel model,
                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            var skillCategory = new SkillCategory();

            skillCategory.Name   = model.Name;
            skillCategory.UserId = model.UserId;
            if (model.Photo != null)
            {
                skillCategory.Photo = Convert.ToBase64String(model.Photo.Buffer);
            }
            skillCategory.CreatedTime = DateTime.Now.ToOADate();

            skillCategory = _dbContext.SkillCategories.Add(skillCategory);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(skillCategory);
        }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> AddSkillCategory([FromBody] AddSkillCategoryViewModel model)
        {
            //Check null for model
            if (model == null)
            {
                model = new AddSkillCategoryViewModel();
                Validate(model);
            }

            // Validate for model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var skillCategories = _unitOfWork.SkillCategories.Search();
            var skillCategory   =
                await skillCategories.FirstOrDefaultAsync(x => x.Name.Equals(model.Name) && x.UserId == model.UserId);

            if (skillCategory != null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict,
                                                                   HttpMessages.SkillCategoryAlreadyAvailable)));
            }

            skillCategory        = new SkillCategory();
            skillCategory.Name   = model.Name;
            skillCategory.UserId = model.UserId;
            if (model.Photo != null)
            {
                skillCategory.Photo = Convert.ToBase64String(model.Photo.Buffer);
            }
            skillCategory.CreatedTime = DateTime.Now.ToOADate();

            //Save to db context
            _unitOfWork.SkillCategories.Insert(skillCategory);

            //save change to db
            await _unitOfWork.CommitAsync();

            return(Ok(skillCategory));
        }