Exemplo n.º 1
0
        public async Task <Spy> CreateAsync(ApiModel.SpyModel spy)
        {
            if (null == spy?.Code || !spy.Code.Any() || string.IsNullOrWhiteSpace(spy.Name))
            {
                throw new ArgumentNullException("spy is missing or invlid.");
            }

            var spies = await _repository.ToListAsync();

            var existingSpy = spies.FirstOrDefault(sp => sp.Name == spy.Name);

            if (null != existingSpy)
            {
                throw new ArgumentException($"A spy with name {spy.Name} already exists.");
            }

            existingSpy = spies.FirstOrDefault(sp => sp.IsCodeEqual(spy.Code));
            if (null != existingSpy)
            {
                throw new ArgumentException($"Another spy with code [{string.Join(',', spy.Code)}] already exists.");
            }

            var newSpy = new Spy
            {
                Code = spy.Code,
                Name = spy.Name
            };

            newSpy = await _repository.CreateAsync(newSpy);

            return(newSpy);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateAsync([FromBody] ApiModel.SpyModel spy)
        {
            var newSpy = await _spyService.CreateAsync(spy);

            return(Ok(newSpy));
        }