Пример #1
0
        public async Task <IActionResult> Post([FromBody] GroupCreateDto @group)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState.Values.Select(value => value.Errors.FirstOrDefault()).FirstOrDefault()?.ErrorMessage));
            }
            if (await _groupsRepository.GetByName(@group.Name) != null)
            {
                return(new BadRequestObjectResult("Group name is already taken"));
            }
            try
            {
                var gr = await _groupsRepository.CreateWithOwner(@group, HttpContext.User.Identity.Name);

                if (group.Users != null)
                {
                    foreach (var user in group.Users)
                    {
                        await _groupsRepository.AddToGroup(user, gr.GroupGuid);
                    }
                }
                return(new OkObjectResult(new GroupFoundDto(gr)));
            }
            catch (ArgumentException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }