Esempio n. 1
0
        public async Task <IActionResult> Edit(GroupServiceModel serviceModel)
        {
            if (ModelState.IsValid)
            {
                var groupTitle = this._groupService
                                 .GetGroupTitle(serviceModel.GroupId);
                if (groupTitle != serviceModel.Title)
                {
                    //Unique title
                    if (await this._groupService.IsTitleExistAsync(serviceModel.Title))
                    {
                        ModelState.AddModelError(
                            "Title",
                            $"Title {serviceModel.Title} already exists. Title must be unique!");

                        return(View(serviceModel));
                    }
                }

                await this._groupService.EditGroupAsync(serviceModel);

                return(RedirectToAction(nameof(JoinedGroups)));
            }
            return(View(serviceModel));
        }
        //Functions
        public Result <GroupServiceModel> Add(GroupServiceModel group, int userId)
        {
            var result = new Result <GroupServiceModel>();

            //initialize transaction
            var transaction = _unitOfWork.BeginTransaction();

            try
            {
                //No mapping. Because system properties shouldn't be null.
                var newGroup = new Group()
                {
                    Name      = group.Name,
                    IsActive  = true,
                    MediaId   = group.MediaId,
                    CreatedOn = Application.CurrentDate,
                    CreatedBy = userId,
                    UpdatedOn = Application.CurrentDate,
                    UpdatedBy = userId
                };
                _unitOfWork.GroupRepository.Add(newGroup);
                _unitOfWork.Save();

                var newGroupUser = new Group_User()
                {
                    GroupId  = newGroup.Id,
                    UserId   = userId,
                    IsActive = true
                };
                _unitOfWork.GroupUserRepository.Add(newGroupUser);
                _unitOfWork.Save();

                //commit transaction
                transaction.commit();

                //return mapped new group
                var mappedNewGroup = _mapper.Map <GroupServiceModel>(newGroup);

                result.IsSuccess = true;
                result.Data      = mappedNewGroup;
                return(result);
            }
            catch (Exception ex)
            {
                //rollback transaction
                transaction.Rollback();
                _logger.LogError(ex.InnerException.Message);
                result.IsSuccess    = false;
                result.ErrorMessage = ErrorMessage.Add;
                return(result);
            }
        }
        public Result <GroupServiceModel> Update(GroupServiceModel groupModel, int userId)
        {
            var result = new Result <GroupServiceModel>();

            try
            {
                var groupEntity = _unitOfWork.GroupRepository.GetSingle(x => x.Id == groupModel.Id && x.IsActive);

                if (groupEntity != null)
                {
                    var group = _mapper.Map <Group>(groupModel);

                    //append system properties as mapper map to null
                    group.UpdatedBy = userId;
                    group.UpdatedOn = Application.CurrentDate;

                    _unitOfWork.GroupRepository.Update(group);
                    _unitOfWork.Save();

                    //map it to GroupServiceModel
                    var updatedGroup = _mapper.Map <GroupServiceModel>(group);
                    result.IsSuccess = true;
                    result.Data      = updatedGroup;
                    return(result);
                }
                else
                {
                    result.IsSuccess    = false;
                    result.ErrorMessage = ErrorMessage.UpdateUnAuth;
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.InnerException.Message);
                result.IsSuccess    = false;
                result.ErrorMessage = ErrorMessage.Update;
                return(result);
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Create(GroupServiceModel serviceModel)
        {
            if (ModelState.IsValid)
            {
                //Unique title
                if (await this._groupService.IsTitleExistAsync(serviceModel.Title))
                {
                    ModelState.AddModelError(
                        "Title",
                        $"Title {serviceModel.Title} already exists. Title must be unique!");

                    return(View(serviceModel));
                }

                serviceModel.AdminId = this._userService
                                       .GetUserId(User);

                await this._groupService.AddGroupAsync(serviceModel);

                return(RedirectToAction(nameof(JoinedGroups)));
            }
            return(View());
        }
Esempio n. 5
0
 public void Update(GroupServiceModel group)
 {
     throw new NotImplementedException();
 }