Exemplo n.º 1
0
        public async Task <JsonResult> AddOrUpdate([FromBody] Direction input)
        {
            await CheckPermission();

            if (input.Id == 0)
            {
                return(Json(await _sqlR.Add(input)));
            }
            return(Json(await _sqlR.Update(input)));
        }
        public async Task <DictionaryCrudResponse> DirectionCrud(DictionaryCrudRequest input)
        {
            var data = input.Direction;

            if (data == null && input.IdToDelete == null)
            {
                throw new ValidationException("Не распарсилось");
            }

            var sqlR = new DirectionsRepository(_logger);

            if (input?.IdToDelete != null)
            {
                await sqlR.Delete((int)input.IdToDelete);

                return(new DictionaryCrudResponse {
                    IsDeleted = true, Direction = data
                });
            }

            var all = await sqlR.GetAll();

            if (all.Any(x => x.Name.Equals(input.Direction.Name)))
            {
                throw new ValidationException(Error.AlreadyAddWithThisName);
            }

            if (data?.Id == 0)
            {
                var item = await sqlR.Add(data);

                return(new DictionaryCrudResponse {
                    IsAdd = true, Direction = item
                });
            }
            else
            {
                var item = await sqlR.Update(data);

                return(new DictionaryCrudResponse {
                    IsUpdated = true, Direction = item
                });
            }
        }