public async Task <ResultEntity> AddOrUpdateAsync(Box box) { var result = new ResultEntity(); var exist = await _boxRepository.GetQueryable().Where(a => a.FrontBn == box.FrontBn && a.PlaceId == box.PlaceId && a.Id != box.Id).CountAsync() > 0; if (exist) { result.Message = "同一交换场所下BN号码不允许重复"; } else { if (box.Id > 0) { _boxRepository.Update(box); } else { await _boxRepository.AddAsync(box); } result.Success = true; } return(result); }
public async Task CreateAsync(string name, string owner) { if (name.Empty()) { throw new ArgumentException("Box name can not be empty.", nameof(name)); } if (owner.Empty()) { throw new ArgumentException("Box owner has not been provided.", nameof(owner)); } var user = await _userRepository.GetAsync(owner); if (user == null) { throw new ArgumentNullException(nameof(user), $"User '{owner}' has not been found."); } if (!user.IsActive) { throw new AuthenticationException($"User '{owner}' is not active."); } var boxes = await GetNamesForUserAsync(owner); var boxesLimit = _featureSettings.BoxesPerUserLimit; if (boxes.Count() >= boxesLimit && user.Role == Role.User) { throw new AuthenticationException($"User '{owner}' can not create more than {boxesLimit} box(es)."); } var entryBox = await _boxRepository.GetAsync(name); if (entryBox != null) { throw new ArgumentException($"Box '{name}' already exists.", nameof(name)); } var box = new Box(name, user); await _boxRepository.AddAsync(box); Logger.Info($"Box '{box.Name}' was created by user '{owner}'."); }