/// <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);
        }
예제 #2
0
        public async Task <IHttpActionResult> EditSkillCategory([FromUri] int id, [FromBody] EditSkillCategoryViewModel model)
        {
            #region Parameter validation

            if (model == null)
            {
                model = new EditSkillCategoryViewModel();
                Validate(model);
            }

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

            Image photo = null;
            if (model.Photo != null)
            {
                var bytes = model.Photo.Buffer;
                photo = _fileService.GetImage(bytes);
                if (photo.Height != ImageSizeConstant.StandardSkillCategoryImageSize ||
                    photo.Width != ImageSizeConstant.StandardSkillCategoryImageSize)
                {
                    ModelState.AddModelError($"{nameof(model)}.{nameof(model.Photo)}", HttpMessages.SkillCategoryImageSizeInvalid);
                    return(BadRequest(ModelState));
                }
            }

            #endregion

            //Get SkillCategory
            var skillCategories = _unitOfWork.SkillCategories.Search();
            skillCategories = skillCategories.Where(x => x.Id == id);
            var skillCategory = await skillCategories.FirstOrDefaultAsync();

            if (skillCategory == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.SkillCategoryNotFound)));
            }

            //Update information
            if (!string.IsNullOrWhiteSpace(model.Name))
            {
                skillCategory.Name = model.Name;
            }

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

                skillCategory.Photo = Url.Content(relativeSkillCategoryImagePath);
            }

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

            return(Ok(skillCategory));
        }
예제 #3
0
        /// <summary>
        /// <inheritdoc />
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task <User> EditUserAsync(int id, EditUserViewModel model, CancellationToken cancellationToken = default(CancellationToken))
        {
            //Find user
            var loadUserCondition = new SearchUserViewModel();

            loadUserCondition.Ids = new HashSet <int>();
            loadUserCondition.Ids.Add(id);

            var user = await GetUserAsync(loadUserCondition, cancellationToken);

            if (user == null)
            {
                throw new Exception(HttpMessages.UserNotFound);
            }

            if (!string.IsNullOrEmpty(model.FirstName))
            {
                user.FirstName = model.FirstName;
            }

            if (!string.IsNullOrEmpty(model.LastName))
            {
                user.LastName = model.LastName;
            }

            if (model.Birthday != null)
            {
                user.Birthday = model.Birthday.Value;
            }

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

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

            //Save to database
            await _unitOfWork.CommitAsync();

            return(user);
        }