Reflections on Developing the Voting Feature

English Version

Overlooked Areas in Development:

  • Did not check if the data conforms to the model before unmarshaling.
  • Failed to verify which projects can be modified based on the registration status.
  • Voting status should be determined using time.

Challenges Encountered:

  • Initially lacked understanding of how data is passed in and returned during development, as well as the related formats.
  • Had a limited grasp of data transformation and its application.
  • Struggled with concatenating the returned data.
  • Was unfamiliar with various validation mechanisms, such as encryption and permission checks.

Learnings:

  • The data types passed in from the frontend are generally int and string. Other types require conversion from string using the unmarshal method. The specific mappings should be specified when creating the struct using the form:"" tag. After data is passed in from the frontend, it can be directly bound using the ShouldBind method, which is very convenient:

    var form JWBVoteForm
    if err := c.ShouldBind(&form); err != nil {
       ...
    }
  • Gained experience in using the unmarshal method and developed a deeper understanding of it. After using marshal/unmarshal methods, it is important to check the results and raise an error if the conversion is incorrect. Data interactions during serialization and deserialization should be in [] types (for standardization purposes):

    type Marshaler interface {
    MarshalJSON() ([]byte, error)
    }

    type Unmarshaler interface {
    UnmarshalJSON([]byte) error
    }
  • Developed a better understanding of the voting mechanism: the voting template stores multiple cover:title key-value pairs. When querying, these can be extracted, converted into a list, and then iterated over to retrieve all the information.

  • Counting votes for a single option can be done directly using an SQL query. By utilizing GROUP BY, the results can be stored in the corresponding struct:

    db.Model(&JWBVoteFormLog{}).Select(count(*) as count , choice as choice).Where("form_id = ? and del = 0",formIDINT).Group(choice).Find(&voteData)
  • When users vote, the option corresponds to an int value, which is the index value of the voting options list in the template. By matching the user's selection (int) with the index of the voting options, the title and cover of the vote can be retrieved.

  • When storing event start and end times in the database, it is important to consider timezone conversion. Data stored in the database is referenced by the database server time, which is 8 hours behind Beijing time. Therefore, when processing daily statistics, the start time needs to be adjusted.

  • Standardized time formatting:

    cstlocGlobal *time.Location
    start, err := time.ParseInLocation("2006-01-02 15:04", start, cstlocaGlobal)

 

开发中忽略的地方:

  1. unmarshal前没有检查数据是否符合模型

  2. 没有基于报名情况查看哪些项目能够修改

  3. 要用时间来判断投票状态

 

遇到的挑战:

  1. 一开始对开发时数据是如何传入、返回以及相关格式不了解

  2. 对数据的转化理解和掌握都有欠缺

  3. 不会拼接返回的数据

  4. 不了解各种检验机制:加密、权限检验等等

 

收获:

  1. 前端传入的数据类型一般为int和string两种,其余的需要unmarshal方法从string中转换。具体对应关系要在建立结构体时用"form:"""指定。数据从前端传入后直接用shouBind方法绑定(感觉十分方便)

    var form JWBVoteForm
    if err := c.ShouldBind(&form); err != nil {
       ...
    }
  2. 积累了unmarshal方法的操作经验,对其有了更深刻的理解。在使用marshal/unmarshal方法后需要对其返回的结果进行判断,如果转换有误需要报错。序列化和反序列化的数据交互都是[]类型(为了标准化)

    type Marshaler interface {
    MarshalJSON() ([]byte, error)
    }

    type Unmarshaler interface {
    UnmarshalJSON([]byte) error
    }

     

  3. 对投票机制有了更深入的理解:投票模板中存入的是多个cover:title键值对,需要查询时将他们提取出来转换为一个list,再对其进行遍历就能获取所有的信息。

  4. 查询单个选项的投票人数可以用sql语句直接完成——利用group,查询出来后存入相应的结构体就可以了。

    db.Model(&JWBVoteFormLog{}).Select(count(*) as count , choice as choice).Where("form_id = ? and del = 0",formIDINT).Group(choice).Find(&voteData)
  5. 用户投票时,选项对应的是一个int值,该值为模板中投票选项list的index值,后续只要将用户选项(int)和投票选项的index对应起来就能查到投票的title和cover。

  6. 活动起止时间入库时要注意时区的转换,数据入库时会参照数据库服务器时间,比北京时间慢8h,所以处理每天的统计量时需要对开始时间处理一下

    标准化时间格式

    cstlocGlobal *time.Location
    start, err := time.ParseInLocation("2006-01-02 15:04", start, cstlocaGlobal)
posted @ 2021-12-15 11:27  临卓  阅读(60)  评论(0)    收藏  举报