public async Task <IActionResult> Create <D>(ControllerBase controller,
                                                     IDbResourceRepository <T> repo, D itemDTO)
        {
            string desc = GetControllerDescription(controller);

            try
            {
                _logger.LogInfo(desc);
                if (itemDTO == null)
                {
                    _logger.LogWarn($"{desc}: Empty request submitted");
                    return(controller.BadRequest());
                }
                if (!controller.ModelState.IsValid)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted");
                    return(controller.BadRequest(controller.ModelState));
                }
                var item = _mapper.Map <T>(itemDTO);

                var isSuccess = await repo.Create(item);

                if (!isSuccess)
                {
                    return(InternalError(controller, $"{desc}: Item creation failed"));
                }
                _logger.LogInfo($"{desc}: Item created");
                return(controller.Created("", new { item }));
            }
            catch (Exception e)
            {
                return(InternalError(controller, e));
            }
        }
        public async Task <IActionResult> Delete(ControllerBase controller,
                                                 IDbResourceRepository <T> repo, int id)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                if (id < 1)
                {
                    _logger.LogWarn($"{desc}: Empty request submitted");
                    return(controller.BadRequest());
                }
                var item = await repo.FindById(id);

                if (item == null)
                {
                    _logger.LogWarn($"{desc}: Item with id ${id} was not found");
                    return(controller.NotFound());
                }

                var isSuccess = await repo.Delete(item);

                if (!isSuccess)
                {
                    return(InternalError(controller, "{desc}: Item delete failed"));
                }
                _logger.LogInfo("{desc}: Item deleted");
                return(controller.Ok(id));
            }
            catch (Exception e)
            {
                return(InternalError(controller, e));
            }
        }
Пример #3
0
        /// <summary>
        /// Get Item by Id
        /// </summary>
        /// <typeparam name="D">Data Transfer Object class</typeparam>
        /// <param name="controller"></param>
        /// <param name="repo"></param>
        /// <param name="id"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <IActionResult> GetItem <D>(ControllerBase controller,
                                                      IDbResourceRepository <T, O> repo, int id, IBaseQueryOptions options)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo($"{desc}: {id}");

                // set options.UserId for the current authenticated user
                var user = await _userService.GetCurrentUser();

                if (user == null)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted - Not a valid user");
                    return(controller.BadRequest());
                }
                options.UserId = user?.Id;

                var item = await repo.GetById(id, options);

                if (item == null)
                {
                    _logger.LogWarn($"{desc}: Item {id} was not found or does not belong to {user?.Email} ");
                    return(controller.NotFound());
                }
                var response = _mapper.Map <D>(item);
                _logger.LogInfo($"{desc} - Successful");
                return(controller.Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError(controller, $"Server Error: {e.Message} - {e.InnerException}"));
            }
        }
Пример #4
0
        public async Task <IActionResult> DeleteItem(ControllerBase controller,
                                                     IDbResourceRepository <T, O> repo, int id)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                if (id < 1)
                {
                    _logger.LogWarn($"{desc}: Empty request submitted");
                    return(controller.BadRequest());
                }
                var user = await _userService.GetCurrentUser();

                if (user == null)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted - Not a valid user");
                    return(controller.BadRequest());
                }
                var exists = await repo.Exists(id, user?.Id);

                if (!exists)
                {
                    _logger.LogWarn($"{desc}: Item with id {id} was not found or does not belong to {user?.Email}");
                    return(controller.NotFound());
                }
                if (!controller.ModelState.IsValid)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted");
                    return(controller.BadRequest(controller.ModelState));
                }



                var item = await repo.GetById(id,
                                              new BaseQueryOptions()
                {
                    UserId = user != null ? user.Id : ""
                });

                if (item == null)
                {
                    _logger.LogWarn($"{desc}: Item with id {id} was not found or does not belong to {user?.Email}");
                    return(controller.NotFound());
                }

                var isSuccess = await repo.Delete(item);

                if (!isSuccess)
                {
                    return(InternalError(controller, "{desc}: Item delete failed"));
                }
                _logger.LogInfo($"{desc}: Item deleted");
                return(controller.Ok(id));
            }
            catch (Exception e)
            {
                return(InternalError(controller, e));
            }
        }
Пример #5
0
        public async Task <IActionResult> UpdateItem <D>(ControllerBase controller,
                                                         IDbResourceRepository <T, O> repo, int id, D itemDTO)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                if (id < 1 || itemDTO == null)
                {
                    _logger.LogWarn($"{desc}: Empty request submitted");
                    return(controller.BadRequest());
                }
                var user = await _userService.GetCurrentUser();

                if (user == null)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted - Not a valid user");
                    return(controller.BadRequest());
                }
                var exists = await repo.Exists(id, user?.Id);

                if (!exists)
                {
                    _logger.LogWarn($"{desc}: Item with id {id} was not found or does not belong to {user?.Email}");
                    return(controller.NotFound());
                }
                if (!controller.ModelState.IsValid)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted");
                    return(controller.BadRequest(controller.ModelState));
                }


                // map data to DTO
                var item = _mapper.Map <T>(itemDTO);
                // force item.Id to be id passed in and userId to currently authenticated user
                item.Id = id;

                // set the userId to the currently authenticated user
                item.UserId = user?.Id;

                var isSuccess = await repo.Update(item);

                if (!isSuccess)
                {
                    return(InternalError(controller, $"{desc}: Update failed"));
                }
                _logger.LogInfo($"{desc}: Update Successful");

                item.UserId = null;

                return(controller.Ok(new { item }));
            }
            catch (Exception e)
            {
                return(InternalError(controller, $"Server Error: {e.Message} - {e.InnerException}"));
            }
        }
        public async Task <ObjectResult> GetItems <D>(ControllerBase controller, IDbResourceRepository <T> repo)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                var items = await repo.FindAll();

                var response = _mapper.Map <IList <D> >(items);
                _logger.LogInfo($"{desc} - Successful");

                return(controller.Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError(controller, e));
            }
        }
        public async Task <IActionResult> Update <D>(ControllerBase controller,
                                                     IDbResourceRepository <T> repo, int id, D itemDTO)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                if (id < 1 || itemDTO == null)
                {
                    _logger.LogWarn($"{desc}: Empty request submitted");
                    return(controller.BadRequest());
                }
                var exists = await repo.Exists(id);

                if (!exists)
                {
                    _logger.LogWarn($"{desc}: Item with id {id} was not found");
                    return(controller.NotFound());
                }
                if (!controller.ModelState.IsValid)
                {
                    _logger.LogWarn($"{desc}: Invalid request submitted");
                    return(controller.BadRequest(controller.ModelState));
                }

                var item = _mapper.Map <T>(itemDTO);
                // force item.Id to be id passed in
                item.Id = id;

                var isSuccess = await repo.Update(item);

                if (!isSuccess)
                {
                    return(InternalError(controller, "{desc}: Update failed"));
                }
                _logger.LogInfo("{desc}: Update Successful");
                return(controller.Ok(new { item }));
            }
            catch (Exception e)
            {
                return(InternalError(controller, $"Server Error: {e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> GetItem <D>(ControllerBase controller, IDbResourceRepository <T> repo, int id)
        {
            try
            {
                string desc = GetControllerDescription(controller);
                _logger.LogInfo($"{desc}: {id}");
                var item = await repo.FindById(id);

                if (item == null)
                {
                    _logger.LogWarn($"{desc}: Item not found: {id}");
                    return(controller.NotFound());
                }
                var response = _mapper.Map <D>(item);
                _logger.LogInfo($"{desc} - Successful");
                return(controller.Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError(controller, $"Server Error: {e.Message} - {e.InnerException}"));
            }
        }
Пример #9
0
        /// <summary>
        /// Get Items
        /// </summary>
        /// <typeparam name="D">Data Transfer Object class</typeparam>
        /// <param name="controller"></param>
        /// <param name="repo"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <ObjectResult> GetItems <D>(ControllerBase controller, IDbResourceRepository <T, O> repo, O options)
        {
            try
            {
                // set options.UserId for the current authenticated user
                var userId = await _userService.GetCurrentUserId();

                options.UserId = userId;
                string desc = GetControllerDescription(controller);
                _logger.LogInfo(desc);
                var items = await repo.Get(options);

                var response = _mapper.Map <IList <D> >(items);
                _logger.LogInfo($"{desc} - Successful");

                return(controller.Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError(controller, e));
            }
        }