public async Task <IActionResult> Add([FromForm] EnsureTaste taste)
        {
            // wait until operation is finished before letting the user continue.
            // discar (_ = ) to prevent unwanted messages from the compiler/analyzer
            _ = await _ensureService.LogAsync(User.GetId(), taste);

            return(RedirectToAction("Logs"));
        }
        public async Task <EnsureLog> LogAsync(string userId, EnsureTaste taste)
        {
            var l = new EnsureLog
            {
                UserId      = userId,
                EnsureTaste = taste
            };

            _dbContext.Logs.Add(l);
            await _dbContext.SaveChangesAsync();

            return(l);
        }
        /// <summary>
        /// Inserts a log to the table async and syncs with server, if network available.
        /// </summary>
        /// <param name="taste">The new log taste</param>
        /// <returns>The newly created log</returns>
        public async Task <InternalEnsureLog> AddLogAsync(EnsureTaste taste)
        {
            InternalEnsureLog log;

            // if net available - push to server
            if (IsInternetConnectionAvailable())
            {
                using (var res = await http.PostAsync($"/api/Ensure/AddLog?taste={(int)taste}"))
                {
                    if (!res.IsSuccessStatusCode) // failure
                    {
                        HandleHttpError(res, true);
                        return(null);
                    }
                    else
                    {
                        log           = JsonConvert.DeserializeObject <InternalEnsureLog>(await res.Content.ReadAsStringAsync());
                        log.SyncState = EnsureSyncState.Synced; // becuase got it from server
                    }
                }
            }
            // if net not available - generate temporary ID until synced.
            else
            {
                log = new InternalEnsureLog()
                {
                    EnsureTaste = taste,
                    Id          = Guid.NewGuid().ToString(),
                    SyncState   = EnsureSyncState.ToAdd,
                    UserId      = ((EnsureApplication)context.ApplicationContext).UserInfo.Id
                };
            }
            OpenDbConnection();
            await db.InsertAsync(log);

            CloseDbConnection();
            return(log);
        }
Exemple #4
0
 /// <summary>
 /// A full constructor. Param doc in props.
 /// </summary>
 public EnsureLog(DateTime logged, EnsureTaste taste, string userId)
 {
     UserId      = userId;
     EnsureTaste = taste;
     Logged      = logged;
 }
 public async Task <ActionResult <EnsureLog> > AddLog(EnsureTaste taste)
 {
     return(await _ensureService.LogAsync(UserExtensions.GetId(User), taste));
 }
 /// <summary>
 /// Set The taste of the currently disaplyed ensure, updating it's UI to match the new taste.
 /// </summary>
 /// <param name="taste"></param>
 public void SetTaste(EnsureTaste taste)
 {
     TasteImage.SetImageResource(TasteImage.Context.Resources.GetIdentifier("ensure_taste_" +
                                                                            taste.ToString().ToLower(), "drawable", TasteImage.Context.PackageName));
     TasteTv.Text = taste.ToString();
 }