public async Task <int> AddProtocol(MongoProtocol protocol, Guid userGuid, int clubId)
        {
            try
            {
                if (protocol == null || userGuid == Guid.Empty)
                {
                    throw new ArgumentException();
                }
                protocol.UserGuid = userGuid;
                protocol.ClubId   = clubId;
                protocol.Added    = DateTimeOffset.UtcNow;
                var result = await _unitOfWork.Protocols.InsertAsync(protocol);

                await _unitOfWork.SaveAsync();

                return(result.Id);
            }
            catch (ArgumentException)
            {
                throw new Exception("Arguments omitted");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                throw new Exception("Error while adding protocol to DB. Please try a bit later");
            }
        }
예제 #2
0
 public async Task <IActionResult> Add([FromBody] MongoProtocol protocol)
 {
     try
     {
         Guid.TryParse(User?.Claims?.SingleOrDefault(t => t.Type == "guid")?.Value, out var guid);
         return(Ok(await _protocolsService.AddProtocol(protocol, guid, 1)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
예제 #3
0
        public async Task <IActionResult> Delete(MongoProtocol protocol)
        {
            try
            {
                await Task.Factory.StartNew(() => _unitOfWork.Protocols.DeleteAsync(protocol))
                .ContinueWith(prevTask => _unitOfWork.SaveAsync());

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest());
            }
        }
예제 #4
0
        public async Task <IActionResult> Update(MongoProtocol protocol)
        {
            try
            {
                if (protocol == null)
                {
                    return(BadRequest("Protocol is empty"));
                }
                var result = await _unitOfWork.Protocols.UpdateAsync(protocol);

                if (result == null)
                {
                    return(NotFound("Error while updating protocol"));
                }
                await Task.Run(() => _unitOfWork.SaveAsync());

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest());
            }
        }