Пример #1
0
        public async Task <IActionResult> Index(EntryViewModel vm)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(vm));
                }

                if (ModelState.IsValid)
                {
                    await _service.AddEntryAsync(vm);

                    ViewBag.Result = new Result {
                        Type = "success", Message = "Entry was saved successfully."
                    };
                    ModelState.Clear();
                    return(View());
                }
                return(View(vm));
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);

                ViewBag.Result = new Result {
                    Type = "danger", Message = "There was some unexpected error, sorry."
                };
                return(View(vm));
            }
        }
Пример #2
0
        public async Task <IActionResult> Post(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "entries")]
            [RequestBodyType(typeof(CreateEntryRequest), "CreateEntryRequest")]
            HttpRequest req,
            ILogger log)
        {
            string authenticatedEmail;

            try
            {
                authenticatedEmail = _tokenService.GetEmailFromBearerToken(req);
            }
            catch (JournallyException ex)
            {
                log.LogWarning($"Authorization error when calling /entries: {ex.Message}");
                return(new UnauthorizedResult());
            }

            if (req.ContentLength <= 0)
            {
                return(HttpCodeHelper.EmptyRequestBody());
            }

            string requestBody;

            using (StreamReader readStream = new StreamReader(req.Body))
            {
                requestBody = await readStream.ReadToEndAsync();
            }

            Entry data;

            try
            {
                data = JsonConvert.DeserializeObject <Entry>(requestBody);
            }
            catch (Exception e)
            {
                return(HttpCodeHelper.Return400(e.Message));
            }

            if (data.DateTime == null)
            {
                return(HttpCodeHelper.Return400("Date provided cannot be null."));
            }

            if (data.Content == null)
            {
                return(HttpCodeHelper.Return400("Content provided cannot be null."));
            }

            var user = await _userService.GetByEmailAsync(authenticatedEmail);

            data.UserId = user.Id;

            await _entryService.AddEntryAsync(data);

            return(new CreatedResult("https://example.com/api/entries/201", data));
        }
        public async Task <IActionResult> AddEntryAsync([FromRoute] int gameId, Entry entryDto)
        {
            // add event to game
            var entryAdded = await _entryService.AddEntryAsync(gameId, entryDto);

            if (_entryService.HasErrors)
            {
                return(new BadRequestObjectResult(new ApiError(_entryService.Errors)));
            }

            return(Ok(entryAdded));
        }
        public async Task <CompetitionEntry> AddEntryAsync(int competitionId, CompetitionEntry competition)
        {
            // convert to entry
            var entryDto    = _mapper.Map <Entry>(competition);
            var dtoMetadata = entryDto.Metadata.ToObject <EntryMetadata>();

            // set the expiry time.
            dtoMetadata = await SetEntryDataExpiryTime(competitionId, dtoMetadata);

            entryDto.Metadata = JObject.FromObject(dtoMetadata);

            var result = await _defaultEntryService.AddEntryAsync(competitionId, entryDto);

            if (_defaultEntryService.HasErrors)
            {
                Errors    = _defaultEntryService.Errors; // just map any errors.
                HasErrors = true;
            }

            var competitionResult = _mapper.Map <CompetitionEntry>(result);

            return(competitionResult);
        }
Пример #5
0
 public async Task <IActionResult> AddEntry([FromBody] Entry entry)
 {
     return(Ok(await _entryService.AddEntryAsync(entry)));
 }