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"); } }
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)); } }
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()); } }
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()); } }