Exemplo n.º 1
0
        public async Task RemoveClient(RemoveClientFromTrainerInputModel input)
        {
            var client = await this.db.Users.FirstOrDefaultAsync(u => u.Email == input.ClientEmail && u.Id == input.ClientId);

            if (client == null)
            {
                throw new ArgumentException("Няма регистриран човек с този е-мейл.");
            }

            if (client.Id == input.TrainerId)
            {
                throw new ArgumentException("Няма как сам да си си треньор :).");
            }

            Trainer trainer = (Trainer)await this.db.Users.FirstOrDefaultAsync(u => u.Id == input.TrainerId);

            if (trainer == null)
            {
                throw new ArgumentException("Невалиден опит. Опитай пак!");
            }

            if (!trainer.Clients.Contains(client))
            {
                throw new InvalidOperationException("Този човек не е твой клиент!");
            }

            trainer.Clients.Remove(client);
            client.Trainer   = null;
            client.TrainerId = null;

            await this.db.SaveChangesAsync();
        }
        public async Task <IActionResult> RemoveClient(RemoveClientFromTrainerInputModel input)
        {
            try
            {
                await this.trainersService.RemoveClient(input);
            }
            catch (Exception e)
            {
                this.ModelState.AddModelError("client", e.Message);
            }

            if (!this.ModelState.IsValid)
            {
                var viewModel = new AddClientToTrainerInputModel()
                {
                    TrainerId = this.User.FindFirstValue(ClaimTypes.NameIdentifier),
                };
                return(this.View(viewModel));
            }

            return(this.Redirect("/Trainer/Trainer/ControlPanel"));
        }