public async Task <GuidDataModel> Add(GuidAPIModel inputModel) { GuidDataModel guidModel = inputModel.ToDataModel(); if (!IsModelValid(guidModel)) { throw new InvalidRequestParameter(Constants.INVALID_MODEL); } //check if guid already exists var item = await _context.GuidList.FindAsync(guidModel.Guid); if (item != null) { throw new RecordAlreadyExists(Constants.GUID_ALREADY_EXISTS); } else { guidModel.Expire = guidModel.Expire; _context.GuidList.Add(guidModel); await _context.SaveChangesAsync(); } return(guidModel); }
public async Task <GuidDataModel> Update(Guid guid, GuidAPIModel updatedModel) { if (updatedModel.Guid.HasValue) { throw new InvalidRequestParameter(Constants.GUID_NOT_UPDATABLE); } GuidDataModel originalGuid = await _context.GuidList.FindAsync(guid); if (originalGuid == null) { throw new RecordNotFound(Constants.GUID_NOT_FOUND); } if (updatedModel.Expire.HasValue) { originalGuid.Expire = updatedModel.Expire.Value; } if (updatedModel.User != null) { originalGuid.User = updatedModel.User; } await _context.SaveChangesAsync(); if (_cache.Get <GuidDataModel>(guid) != null) { _cache.Delete(guid); } _cache.Add <GuidDataModel>(guid, originalGuid); return(originalGuid); }
public bool IsModelValid(GuidDataModel guidModel) { if (guidModel.Expire == 0 && guidModel.User == null && guidModel.Guid.Equals(Guid.Empty)) { return(false); } return(true); }
public void Delete(Guid guid) { GuidDataModel existingGuid = _context.GuidList.Find(guid); if (existingGuid == null) { throw new RecordNotFound(Constants.GUID_NOT_FOUND); } else { _context.GuidList.Remove(existingGuid); _context.SaveChanges(); _cache.Delete(guid); } }
public async Task <ActionResult <GuidAPIModel> > PostGuid(GuidAPIModel inputModel) { GuidDataModel guidModel = await _manager.Add(inputModel); return(guidModel.ToAPIModel()); }
public async Task <ActionResult <GuidAPIModel> > GetGuid(Guid guid) { GuidDataModel guidItem = await _manager.Get(guid); return(guidItem.ToAPIModel()); }