Example #1
0
        public async void CreateGenreTest()
        {
            var id       = RandomId.NewId();
            var cashback = new List <CashbackCommand>();

            cashback.Add(new CashbackCommand()
            {
                Id = null, Percent = 15, DayOfWeek = System.DayOfWeek.Monday, GenreId = id
            });
            cashback.Add(new CashbackCommand()
            {
                Id = null, Percent = 10, DayOfWeek = System.DayOfWeek.Saturday, GenreId = id
            });
            cashback.Add(new CashbackCommand()
            {
                Id = null, Percent = 5, DayOfWeek = System.DayOfWeek.Sunday, GenreId = id
            });

            var cmd = new CreateGenreCommand()
            {
                Id       = id,
                Name     = "Test Genre 1",
                Cashback = cashback
            };

            var result = await CommandsHandler.Handle(cmd);

            var obj = await DbContext.Genres.Where(w => w.Id == id).FirstOrDefaultAsync();

            Assert.NotNull(result);
            Assert.NotNull(obj);
            Assert.Equal(ErrorCode.None, result.ErrorCode);
            Assert.Equal(id, obj.Id);
            Assert.True(result.Rows > 0);
        }
Example #2
0
        public async Task <IActionResult> CreateGenre([FromBody] CreateGenreCommand command)
        {
            await _mediator.Send(command, HttpContext.RequestAborted);

            return(Ok());
        }
Example #3
0
 public async Task <IActionResult> SaveNewGenre(CreateGenreCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }
Example #4
0
        public async Task <CommandResult> ExecuteAsync(CashbackCommandsHandler handler)
        {
            if (handler.DbContext.Genres != null && handler.DbContext.Genres.Count() > 0)
            {
                return(await Task.FromResult(new CommandResult(ErrorCode.None)));
            }

            if (string.IsNullOrEmpty(AccessToken))
            {
                Authorize();
            }

            IList <GenreViewModel> result = new List <GenreViewModel>();
            var defaultCashback           = new DefaultCashback();

            HttpClient httpClient = new HttpClient();
            var        genresUrls = new[] {
                "browse/categories/pop?country=BR",
                "browse/categories/mpb?country=BR",
                "browse/categories/classical?country=BR",
                "browse/categories/rock?country=BR"
            };

            var rows = 0;

            HttpResponseMessage response = null;

            foreach (var url in genresUrls)
            {
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);
                response = await httpClient.GetAsync("https://api.spotify.com/v1/" + url);

                var json = JsonConvert.DeserializeObject <GenreViewModel>(await response.Content.ReadAsStringAsync());

                if (response.IsSuccessStatusCode && !result.Any(c => c.Name == json.Name))
                {
                    result.Add(json);
                }
            }

            foreach (var item in result)
            {
                var id       = RandomId.NewId();
                var cashback = new List <CashbackCommand>();
                var lst      = defaultCashback.GetDefaultCashback(id, item.Name);
                lst.ForEach(f => cashback.Add(new CashbackCommand()
                {
                    Id = f.Id, GenreId = f.GenreId, DayOfWeek = f.DayOfWeek, Percent = f.Percent
                }));

                var cmd = new CreateGenreCommand()
                {
                    Id       = id,
                    Name     = item.Name,
                    Cashback = cashback
                };

                var rs = await handler.Handle(cmd);

                rows += rs.Rows;
            }

            return(await Task.FromResult(new CommandResult(rows, ErrorCode.None)));
        }
Example #5
0
 public GenresController()
 {
     _createGenreCommand = new CreateGenreCommand();
     _getAllGenresQuery  = new GetAllGenresQuery();
 }
Example #6
0
        public async Task <IActionResult> Create(CreateGenreCommand Genre)
        {
            await Mediator.Send(Genre);

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Post([FromBody] CreateGenreCommand command)
        {
            var id = await _mediator.Send(command);

            return(CreatedAtAction(nameof(GetById), new { id = id }, command));
        }
Example #8
0
        public async Task <GenreDto> CreateGenre(CreateGenreCommand command)
        {
            var genre = await this.Add(new Genre { Name = command.Name });

            return(genre.ToDto());
        }
Example #9
0
 public async Task <IHttpActionResult> Create([FromBody] CreateGenreCommand command)
 {
     return(await this.TryCreate(async() => await this._genreRepository.CreateGenre(command)));
 }