public async Task <IActionResult> Remove(int id, [FromBody] TimeGroupApi item) { if (!item.Date.HasValue) { ModelState.AddModelError("Date", "Required Date is null or empty."); return(new BadRequestObjectResult(ModelState)); } var group = await DbContext.Groups.Where(g => g.PartitionId == CurrentPartitionId && g.Id == id).FirstOrDefaultAsync(); if (group == null) { throw new NotFoundResultException($"Group (GroupId:{id}) not found."); } await RemoveSubElements(id, item.Date.Value); var defaultTaskGroup = await DbContext.DefaultGroups.Where(tdg => tdg.UserId == CurrentUserId && tdg.GroupId == id).FirstOrDefaultAsync(); if (defaultTaskGroup != null) { DbContext.DefaultGroups.Remove(defaultTaskGroup); } await DbContext.SaveChangesAsync(); return(Ok()); }
private async Task AddToDefaultGroupIfNotExists(long id, TimeGroupApi item, Group group) { if (item.Date.HasValue && item.Date.Value.Date == DateTime.Now.Date) { if (!(await DbContext.DefaultGroups.Where(td => td.UserId == CurrentUserId && td.GroupId == id).AnyAsync())) { DbContext.DefaultGroups.Add(new DefaultGroup { UserId = CurrentUserId, Group = group }); } } }
public async Task <IActionResult> Post([FromBody] TimeGroupApi item) { await CleanWhiteSpace(item); if (!ModelState.IsValidUpdated()) { return(new BadRequestObjectResult(ModelState)); } var group = await DbContext.Groups.Where(g => g.PartitionId == CurrentPartitionId && g.Name.Equals(item.Name, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefaultAsync(); if (group == null) { group = new Group { PartitionId = CurrentPartitionId, Name = item.Name }; DbContext.Groups.Add(group); if (item.Date.HasValue && item.Date.Value.Date == DateTime.Now.Date) { DbContext.DefaultGroups.Add(new DefaultGroup { UserId = CurrentUserId, Group = group }); } await DbContext.SaveChangesAsync(); } else { await MapDeltaValuesAndCleanWhiteSpace(item, group); if (!ModelState.IsValidUpdated()) { return(new BadRequestObjectResult(ModelState)); } await AddToDefaultGroupIfNotExists(group.Id, item, group); await DbContext.SaveChangesAsync(); } item.Id = group.Id; item.Name = group.Name; return(Json(item)); }
public async Task <IActionResult> Patch(long id, [FromBody] TimeGroupApi item) { var group = await DbContext.Groups.Where(g => g.PartitionId == CurrentPartitionId && g.Id == id).FirstOrDefaultAsync(); if (group == null) { throw new NotFoundResultException($"Group (GroupId:{id}) not found."); } await MapDeltaValuesAndCleanWhiteSpace(item, group); if (!ModelState.IsValidUpdated()) { return(new BadRequestObjectResult(ModelState)); } await AddToDefaultGroupIfNotExists(id, item, group); await DbContext.SaveChangesAsync(); item.Id = id; return(Json(item)); }
private string GroupTaskUniquId(DateTime date, string[] changedUniqIds, TimeGroupApi group, TimeTaskApi task) { var uniquId = $"g{group.Id}t{task.Id}{(task.Item?.Id != null ? $"ti{task.Item?.Id}" : "")}"; return(ChangedUniquId(date, changedUniqIds, uniquId)); }
private string GroupUniquId(DateTime date, string[] changedUniqIds, TimeGroupApi group) { var uniquId = $"g{group.Id}"; return(ChangedUniquId(date, changedUniqIds, uniquId)); }