Exemplo n.º 1
0
        /// <summary>
        /// Checks Name
        /// </summary>
        /// <param name="viewModel">Injected <see cref="AddApplicationRole"/></param>
        /// <returns>Instance of <see cref="Task{ApplicationRole}"/></returns>
        public async Task <ApplicationRole> CheckName(AddApplicationRole @viewModel)
        {
            ApplicationRole @applicationRole = await Context.ApplicationRole
                                               .AsNoTracking()
                                               .TagWith("CheckName")
                                               .FirstOrDefaultAsync(x => x.Name == @viewModel.Name);

            if (@applicationRole != null)
            {
                // Log
                string @logData = @applicationRole.GetType().Name
                                  + " with Name "
                                  + @applicationRole.Name
                                  + " was already found at "
                                  + DateTime.Now.ToShortTimeString();

                Logger.WriteGetItemFoundLog(@logData);

                throw new Exception(@applicationRole.GetType().Name
                                    + " with Name "
                                    + viewModel.Name
                                    + " already exists");
            }

            return(@applicationRole);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds Application Role By Id
        /// </summary>
        /// <param name="id">Injected <see cref="int"/></param>
        /// <returns>Instance of <see cref="Task{ApplicationRole}"/></returns>
        public async Task <ApplicationRole> FindApplicationRoleById(int @id)
        {
            ApplicationRole @applicationRole = await Context.ApplicationRole.
                                               TagWith("FindApplicationRoleById")
                                               .FirstOrDefaultAsync(x => x.Id == @id);

            if (@applicationRole == null)
            {
                // Log
                string @logData = @applicationRole.GetType().Name
                                  + " with Id "
                                  + @id
                                  + " was not found at "
                                  + DateTime.Now.ToShortTimeString();

                Logger.WriteGetItemNotFoundLog(@logData);

                throw new Exception(@applicationRole.GetType().Name
                                    + " with Id "
                                    + id
                                    + " does not exist");
            }

            return(@applicationRole);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Updates Application Role
        /// </summary>
        /// <param name="viewModel">Injected <see cref="UpdateApplicationRole"/></param>
        /// <returns>Instance of <see cref="Task{ViewApplicationRole}"/></returns>
        public async Task <ViewApplicationRole> UpdateApplicationRole(UpdateApplicationRole @viewModel)
        {
            await CheckName(@viewModel);

            ApplicationRole @applicationRole = await FindApplicationRoleById(@viewModel.Id);

            @applicationRole.Name           = @viewModel.Name;
            @applicationRole.NormalizedName = @viewModel.Name;
            @applicationRole.ImageUri       = @viewModel.ImageUri;

            Context.ApplicationRole.Update(@applicationRole);

            await Context.SaveChangesAsync();

            // Log
            string @logData = @applicationRole.GetType().Name
                              + " with Id "
                              + @applicationRole.Id
                              + " was modified at "
                              + DateTime.Now.ToShortTimeString();

            Logger.WriteUpdateItemLog(@logData);

            return(Mapper.Map <ViewApplicationRole>(@applicationRole));
        }
Exemplo n.º 4
0
        async public Task <T> UpdatePropertyAsync <T>(ApplicationRole role, string applicationRoleProperty, T propertyValue, CancellationToken cancellation)
        {
            var propertyInfo = role.GetType().GetProperty(applicationRoleProperty);

            if (propertyInfo != null)
            {
                propertyInfo.SetValue(role, propertyValue);

                await UpdateAsync(role, cancellation);
            }

            return(propertyValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Removes Application Role By Id
        /// </summary>
        /// <param name="id">Injected <see cref="int"/></param>
        /// <returns>Instance of <see cref="Task"/></returns>
        public async Task RemoveApplicationRoleById(int @id)
        {
            ApplicationRole @applicationRole = await FindApplicationRoleById(@id);

            Context.ApplicationRole.Remove(@applicationRole);

            await Context.SaveChangesAsync();

            // Log
            string @logData = @applicationRole.GetType().Name
                              + " with Id "
                              + @applicationRole.Id
                              + " was removed at "
                              + DateTime.Now.ToShortTimeString();

            Logger.WriteDeleteItemLog(@logData);
        }