public override async Task <GasLog> UpdateAsync(GasLog entity) { Guard.Against <ArgumentNullException>(entity == null, nameof(entity)); // ReSharper disable once PossibleNullReferenceException var curLog = await GetEntityByIdAsync(entity.Id); if (curLog == null) { ProcessResult.AddErrorMessage("Cannot find automobile in data source"); return(null); } var curNote = GetUpdateLogNote(curLog, entity); await NoteManager.UpdateAsync(curNote); if (!NoteManager.ProcessResult.Success) { return(null); } var updatedLog = await GetEntityByIdAsync(curLog.Id); return(updatedLog); }
public override async Task <GasLog> CreateAsync(GasLog entity) { Guard.Against <ArgumentNullException>(entity == null, nameof(entity)); // ReSharper disable once PossibleNullReferenceException entity.AuthorId = DefaultAuthor.Id; entity.CreateDate = _dateProvider.UtcNow; // ToDo: validate current meter reader with auto meter reader if (!Validator.IsValidEntity(entity, ProcessResult)) { return(null); } var auto = await _autoManager.GetEntityByIdAsync(entity.Car.Id); if (auto == null) { ProcessResult.AddErrorMessage("Cannot find automobile information"); return(null); } var(isMeterValid, validationError) = MeterReadingValid(entity, auto); if (!isMeterValid) { ProcessResult.AddErrorMessage(validationError); return(null); } if (!string.IsNullOrEmpty(validationError)) { ProcessResult.AddWaningMessage(validationError); } // Update automobile meter reader auto.MeterReading = (long)entity.CurrentMeterReading.TotalKilometre; var updatedAuto = await _autoManager.UpdateAsync(auto); if (updatedAuto == null) { ProcessResult.PropagandaResult(_autoManager.ProcessResult); return(null); } // ReSharper disable once PossibleNullReferenceException var note = entity.GetNote(NoteSerializer, DefaultAuthor); await NoteManager.CreateAsync(note); switch (NoteManager.ProcessResult.Success) { case false: ProcessResult.PropagandaResult(NoteManager.ProcessResult); return(null); default: var newLog = await GetEntityByIdAsync(note.Id); return(newLog); } }
public override GasLog Update(GasLog entity) { Guard.Against <ArgumentNullException>(entity == null, nameof(entity)); // ReSharper disable once PossibleNullReferenceException var curLog = GetEntityById(entity.Id); if (curLog == null) { ProcessResult.AddErrorMessage("Cannot find gas log in data source"); return(null); } var curNote = GetUpdateLogNote(curLog, entity); NoteManager.Update(curNote); switch (NoteManager.ProcessResult.Success) { case false: ProcessResult.PropagandaResult(NoteManager.ProcessResult); return(null); default: return(GetEntityById(curLog.Id)); } }
public async Task <HmmNote> UpdateAsync(HmmNote note) { if (!_validator.IsValidEntity(note, ProcessResult)) { return(null); } // make sure not update note which get cached in current session var curNote = await GetNoteByIdAsync(note.Id); if (curNote == null) { ProcessResult.AddErrorMessage($"Cannot find note {note.Id} "); return(null); } curNote.Subject = note.Subject; curNote.Content = note.Content; curNote.IsDeleted = note.IsDeleted; curNote.Description = note.Description; curNote.LastModifiedDate = _dateProvider.UtcNow; var ret = await _noteRepo.UpdateAsync(curNote); if (ret == null) { ProcessResult.PropagandaResult(_noteRepo.ProcessMessage); } return(ret); }
public async Task <Author> UpdateAsync(Author authorInfo) { try { if (!_validator.IsValidEntity(authorInfo, ProcessResult)) { return(null); } var authorExists = await AuthorExistsAsync(authorInfo.Id.ToString()); if (!authorExists) { ProcessResult.AddErrorMessage($"Cannot update author: {authorInfo.AccountName}, because system cannot find it in data source"); return(null); } var updatedUser = await _authorRepo.UpdateAsync(authorInfo); if (updatedUser == null) { ProcessResult.PropagandaResult(_authorRepo.ProcessMessage); } return(updatedUser); } catch (Exception ex) { ProcessResult.WrapException(ex); return(null); } }
public NoteCatalog Update(NoteCatalog catalog) { if (catalog == null || !_validator.IsValidEntity(catalog, ProcessResult)) { return(null); } // check if note render exists in data source if (_lookupRepo.GetEntity <NoteRender>(catalog.Render.Id) == null) { ProcessResult.AddErrorMessage($"Cannot update catalog: {catalog.Name}, because note render does not exists in data source"); return(null); } // update catalog record var savedCatalog = _dataSource.GetEntity(catalog.Id); if (savedCatalog == null) { ProcessResult.AddErrorMessage($"Cannot update catalog: {catalog.Name}, because system cannot find it in data source"); return(null); } var updatedCatalog = _dataSource.Update(catalog); if (updatedCatalog == null) { ProcessResult.PropagandaResult(_dataSource.ProcessMessage); } return(updatedCatalog); }
/// <summary> /// The asynchronous version of <see cref="GetNotes"/> /// </summary> /// <param name="entity">The entity which used to get type and figure out the catalog</param> /// <param name="resourceCollectionParameters">The page information of the resource collection</param> /// <returns>The notes which belongs to entity type</returns> protected async Task <PageList <HmmNote> > GetNotesAsync(T entity, Expression <Func <HmmNote, bool> > query = null, ResourceCollectionParameters resourceCollectionParameters = null) { var catId = await entity.GetCatalogIdAsync(LookupRepo); var author = await LookupRepo.GetEntityAsync <Author>(DefaultAuthor.Id); var hasValidAuthor = DefaultAuthor != null && author != null; switch (hasValidAuthor) { case false: ProcessResult.AddErrorMessage("Cannot find default author", true); return(null); default: PageList <HmmNote> notes; if (query != null) { var finalExp = query.And(n => n.Author.Id == DefaultAuthor.Id && n.Catalog.Id == catId); notes = await NoteManager.GetNotesAsync(finalExp, false, resourceCollectionParameters); } else { notes = await NoteManager.GetNotesAsync(n => n.Author.Id == DefaultAuthor.Id && n.Catalog.Id == catId, false, resourceCollectionParameters); } return(notes); } }
/// <summary> /// Get notes for specific entity /// </summary> /// <param name="id">The id of entity to get type and figure out the catalog</param> /// <param name="entity">The entity by id which used to get type and figure out the catalog</param> /// <returns>The notes which belongs to entity type</returns> protected async Task <HmmNote> GetNoteAsync(int id, T entity) { var catId = await entity.GetCatalogIdAsync(LookupRepo); var hasValidAuthor = DefaultAuthor != null && LookupRepo.GetEntity <Author>(DefaultAuthor.Id) != null; switch (hasValidAuthor) { case false: ProcessResult.AddErrorMessage("Cannot find default author", true); return(null); default: var note = await NoteManager.GetNoteByIdAsync(id); if (note == null) { return(null); } if (note.Author.Id == DefaultAuthor.Id && note.Catalog.Id == catId) { return(note); } return(null); } }
public override async Task <GasDiscount> UpdateAsync(GasDiscount entity) { Guard.Against <ArgumentNullException>(entity == null, nameof(entity)); // ReSharper disable once PossibleNullReferenceException var curDiscount = await GetEntityByIdAsync(entity.Id); if (curDiscount == null) { ProcessResult.AddErrorMessage("Cannot find discount in data source"); return(null); } curDiscount.Amount = entity.Amount; curDiscount.Comment = entity.Comment; curDiscount.DiscountType = entity.DiscountType; curDiscount.IsActive = entity.IsActive; curDiscount.Program = entity.Program; var curNote = curDiscount.GetNote(NoteSerializer, DefaultAuthor); await NoteManager.UpdateAsync(curNote); switch (NoteManager.ProcessResult.Success) { case false: ProcessResult.PropagandaResult(NoteManager.ProcessResult); return(null); default: var updDiscount = await GetEntityByIdAsync(curDiscount.Id); return(updDiscount); } }
public Appointment Create(Appointment appointment) { if (appointment == null) { ProcessResult.AddErrorMessage("Null object found for appointment creating"); return(null); } if (!_validator.IsValidEntity(appointment, ProcessResult)) { return(null); } appointment.Id = Guid.NewGuid(); var savedApp = _appointRepo.Add(appointment); switch (savedApp) { case null: ProcessResult.PropagandaResult(_appointRepo.ProcessMessage); return(null); default: return(savedApp); } }
private List <GasDiscountInfo> GetDiscountInfos(XContainer discountRoot, XNamespace ns) { var infos = new List <GasDiscountInfo>(); if (discountRoot == null) { return(infos); } foreach (var element in discountRoot.Elements()) { var amountNode = element.Element(ns + "Amount"); if (amountNode == null) { ProcessResult.AddErrorMessage("Cannot found money information from discount string"); continue; } var money = amountNode.GetMoney(); var discountIdStr = element.Element(ns + "Program")?.Value; if (!int.TryParse(discountIdStr, out var discountId)) { ProcessResult.AddErrorMessage($"Cannot found valid discount id from string {discountIdStr}"); continue; } // Setup temporary discount object with right id, we cannot get discount entity right now because DB reader // is still opening for GasLog var discount = _discountManager.GetEntityById(discountId); if (discount == null) { ProcessResult.AddErrorMessage($"Cannot found discount id : {discountId} from data source"); continue; } infos.Add(new GasDiscountInfo { Amount = money, Program = discount }); } return(infos); }
public async Task <bool> DeleteAsync(int noteId) { var note = await GetNoteByIdAsync(noteId); if (note == null) { ProcessResult.AddErrorMessage($"Cannot find note with id {noteId}", true); return(false); } note.IsDeleted = true; var deletedNote = await UpdateAsync(note); if (deletedNote != null) { return(true); } ProcessResult.PropagandaResult(_noteRepo.ProcessMessage); return(false); }
public async Task DeActivateAsync(Guid id) { var user = await _authorRepo.GetEntityAsync(id); if (user == null) { ProcessResult.Success = false; ProcessResult.AddErrorMessage($"Cannot find user with id : {id}", true); } else if (user.IsActivated) { try { user.IsActivated = false; await _authorRepo.UpdateAsync(user); } catch (Exception ex) { ProcessResult.WrapException(ex); } } }
public override string GetNoteSerializationText(GasLog entity) { if (entity == null) { return(string.Empty); } var xml = new XElement(AutomobileConstant.GasLogRecordSubject, new XElement("Date", entity.Date.ToString("o")), new XElement("Distance", entity.Distance.SerializeToXml(ContentNamespace)), new XElement("CurrentMeterReading", entity.CurrentMeterReading.SerializeToXml(ContentNamespace)), new XElement("Gas", entity.Gas.SerializeToXml(ContentNamespace)), new XElement("Price", entity.Price.SerializeToXml(ContentNamespace)), new XElement("GasStation", entity.Station), new XElement("Discounts", ""), new XElement("Automobile", entity.Car.Id), new XElement("Comment", entity.Comment ?? ""), new XElement("CreateDate", entity.CreateDate.ToString("o"))); if (entity.Discounts.Any()) { foreach (var disc in entity.Discounts) { if (disc.Amount == null || disc.Program == null) { ProcessResult.AddErrorMessage("Cannot found valid discount information, amount or discount program is missing"); continue; } var discElement = new XElement("Discount", new XElement("Amount", disc.Amount?.SerializeToXml(ContentNamespace)), new XElement("Program", disc.Program?.Id)); xml.Element("Discounts")?.Add(discElement); } } return(GetNoteContent(xml).ToString(SaveOptions.DisableFormatting)); }
public async Task <Subsystem> CreateAsync(Subsystem system) { if (!_validator.IsValidEntity(system, ProcessResult)) { return(null); } try { if (HasApplicationRegistered(system)) { ProcessResult.AddErrorMessage($"The application {system.Name} is existed in system"); return(null); } // Add default author to system if (system.DefaultAuthor == null) { ProcessResult.AddErrorMessage($"The application {system.Name}'s default author is null or invalid"); return(null); } // Add sub system to system var addedSystem = await _dataSource.AddAsync(system); if (addedSystem == null) { ProcessResult.PropagandaResult(_dataSource.ProcessMessage); } return(addedSystem); } catch (Exception ex) { ProcessResult.WrapException(ex); return(null); } }
public async Task <NoteRender> UpdateAsync(NoteRender render) { if (!_validator.IsValidEntity(render, ProcessResult)) { return(null); } // make sure the render exists in system var savedRender = await _dataSource.GetEntityAsync(render.Id); if (savedRender == null) { ProcessResult.AddErrorMessage($"Cannot update render: {render.Name}, because system cannot find it in data source"); return(null); } var updatedRender = await _dataSource.UpdateAsync(render); if (updatedRender == null) { ProcessResult.PropagandaResult(_dataSource.ProcessMessage); } return(updatedRender); }